非常教程

jQuery参考手册

jQuery – AJAX get() 和 post() 方法

jQuery – AJAX get() 和 post() 方法

jQuery - AJAX get() 和 post() 方法


jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。


HTTP 请求:GET vs. POST

两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。

  • GET - 从指定的资源请求数据
  • POST - 向指定的资源提交要处理的数据

GET 基本上用于从服务器获得(取回)数据。注释:GET 方法可能返回缓存数据。

POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据。

如需学习更多有关 GET 和 POST 以及两方法差异的知识,请阅读我们的 HTTP 方法 - GET 对比 POST。


jQuery $.get() 方法

$.get() 方法通过 HTTP GET 请求从服务器上请求数据。

语法:

$.get(URL,callback);

必需的 URL 参数规定您希望请求的 URL。

可选的 callback 参数是请求成功后所执行的函数名。

下面的例子使用 $.get() 方法从服务器上的一个文件中取回数据:

实例

$("button").click(function(){ $.get("demo_test.php",function(data,status){ alert("数据: " + data + "\n状态: " + status); }); });

尝试一下 »

$.get() 的第一个参数是我们希望请求的 URL("demo_test.php")。

第二个参数是回调函数。第一个回调参数存有被请求页面的内容,第二个回调参数存有请求的状态。

提示: 这个 PHP 文件 ("demo_test.php") 类似这样:

demo_test.php 文件代码:

<?php echo '这是个从PHP文件中读取的数据。'; ?>


jQuery $.post() 方法

$.post() 方法通过 HTTP POST 请求向服务器提交数据。

语法:

$.post(URL,data,callback);

必需的 URL 参数规定您希望请求的 URL。

可选的 data 参数规定连同请求发送的数据。

可选的 callback 参数是请求成功后所执行的函数名。

下面的例子使用 $.post() 连同请求一起发送数据:

实例

$("button").click(function(){ $.post("/try/ajax/demo_test_post.php", { name:"非常教程", url:"http://www.verydoc.net" }, function(data,status){ alert("数据: \n" + data + "\n状态: " + status); }); });

尝试一下 »

$.post() 的第一个参数是我们希望请求的 URL ("demo_test_post.php")。

然后我们连同请求(name 和 url)一起发送数据。

"demo_test_post.php" 中的 PHP 脚本读取这些参数,对它们进行处理,然后返回结果。

第三个参数是回调函数。第一个回调参数存有被请求页面的内容,而第二个参数存有请求的状态。

提示: 这个 PHP 文件 ("demo_test_post.php") 类似这样:

demo_test_post.php 文件代码:

<?php $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; $url = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : ''; echo '网站名: ' . $name; echo "\n"; echo 'URL 地址: ' .$url; ?>
jQuery – AJAX get() 和 post() 方法

jQuery目录

1.jQuery 教程
2.jQuery 简介
3.jQuery 安装
4.jQuery 语法
5.jQuery 选择器
6.jQuery 效果 – 隐藏和显示
7.jQuery 事件
8.jQuery 效果 – 滑动
9.jQuery 效果 – 淡入淡出
10.jQuery 链
11.jQuery Callback 方法
12.jQuery 效果 – 停止动画
13.jQuery 效果 – 动画
14.jQuery 遍历 – 后代
15.jQuery 遍历 – 祖先
16.jQuery 遍历
17.jQuery 尺寸
18.jQuery css() 方法
19.jQuery 获取并设置 CSS 类
20.jQuery 删除元素
21.jQuery 添加元素
22.jQuery 设置内容和属性
23.jQuery 获取内容和属性
24.jQuery 实例
25.jQuery noConflict() 方法
26.jQuery – AJAX get() 和 post() 方法
27.jQuery – AJAX load() 方法
28.jQuery AJAX 简介
29.jQuery 遍历 – 过滤
30.jQuery 遍历 – 同胞(siblings)
31.jQuery :even 选择器
32.jQuery :last 选择器
33.jQuery :first 选择器
34.jQuery 多个元素选择器
35.jQuery element 选择器
36.jQuery 多个类选择器
37.jQuery .class 选择器
38.jQuery #id 选择器
39.jQuery * 选择器
40.jQuery :only-child 选择器
41.jQuery :nth-last-of-type() 选择器
42.jQuery :nth-of-type() 选择器
43.jQuery :nth-last-child() 选择器
44.jQuery :nth-child() 选择器
45.jQuery :last-of-type 选择器
46.jQuery :last-child 选择器
47.jQuery :first-of-type 选择器
48.jQuery :first-child 选择器
49.jQuery :odd 选择器
50.jQuery :header 选择器
51.jQuery :not() 选择器
52.jQuery :lt() 选择器
53.jQuery :gt() 选择器
54.jQuery :eq() 选择器
55.jQuery element ~ siblings 选择器
56.jQuery element + next 选择器
57.jQuery 父后代选择器
58.jQuery parent > child 选择器
59.jQuery :only-of-type 选择器
60.jQuery :lang() 选择器
61.jQuery :root 选择器
62.jQuery :visible 选择器
63.jQuery :hidden 选择器
64.jQuery :parent 选择器
65.jQuery :empty 选择器
66.jQuery :has() 选择器
67.jQuery :contains() 选择器
68.jQuery :focus 选择器
69.jQuery :animated 选择器
70.jQuery :input 选择器
71.jQuery [attribute*=value] 选择器
72.jQuery [attribute~=value] 选择器
73.jQuery [attribute^=value] 选择器
74.jQuery [attribute|=value] 选择器
75.jQuery [attribute$=value] 选择器
76.jQuery [attribute!=value] 选择器
77.jQuery [attribute=value] 选择器
78.jQuery [attribute] 选择器
79.jQuery :image 选择器
80.jQuery :button 选择器
81.jQuery :reset 选择器
82.jQuery :submit 选择器
83.jQuery :checkbox 选择器
84.jQuery :radio 选择器
85.jQuery :password 选择器
86.jQuery :text 选择器
87.jQuery dblclick() 方法
88.jQuery click() 方法
89.jQuery change() 方法
90.jQuery blur() 方法
91.jQuery bind() 方法
92.jQuery :checked 选择器
93.jQuery :selected 选择器
94.jQuery :disabled 选择器
95.jQuery :enabled 选择器
96.jQuery :file 选择器
97.jQuery event.namespace 属性
98.jQuery event.isPropagationStopped() 方法
99.jQuery event.isImmediatePropagationStopped() 方法
100.jQuery event.isDefaultPrevented() 方法