小白教程
所有教程
关于
Search
108.162.216.35
108.162.216.35
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JQuery – AJAX get() 和 post() 方法”的源代码
本页内容
上一节:
JQuery_–_AJAX_load()_方法
下一节:
JQuery_noConflict()_方法
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:jQuery get()/post() 方法}}[[Category:jQuery 教程|28]] = jQuery - AJAX get() 和 post() 方法 = jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。 == HTTP 请求:GET vs POST == 两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。 * GET - 从指定的资源请求数据 * POST - 向指定的资源提交要处理的数据 GET 基本上用于从服务器获得(取回)数据。注释:GET 方法可能返回缓存数据。 POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据。 如需学习更多有关 GET 和 POST 以及两方法差异的知识,请阅读我们的 [[html httpmethods|HTTP 方法 - GET 对比 POST]]。 == jQuery $.get() 方法 == $.get() 方法通过 HTTP GET 请求从服务器上请求数据。 === 语法: === <pre>$.get(URL,callback); 或 $.get( URL [, data ] [, callback ] [, dataType ] )</pre> * '''URL''':发送请求的 URL字符串。 * '''data''':可选的,发送给服务器的字符串或 key/value 键值对。 * '''callback''':可选的,请求成功后执行的回调函数。 * '''dataType''':可选的,从服务器返回的数据类型。默认:智能猜测(可以是xml, json, script, 或 html)。 下面的例子使用 $.get() 方法从服务器上的一个文件中取回数据: <sample title="" desc="" lang="html" hererun="1"> $("button").click(function(){ $.get("demo_test.php",function(data,status){ alert("数据: " + data + "\n状态: " + status); }); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $.get("/try/ajax/demo_test.php",function(data,status){ alert("数据: " + data + "\n状态: " + status); }); }); }); </script> </head> <body> <button>发送一个 HTTP GET 请求并获取返回结果</button> </body> </html> </run> $.get() 的第一个参数是我们希望请求的 URL("demo_test.php")。 第二个参数是回调函数。第一个回调参数存有被请求页面的内容,第二个回调参数存有请求的状态。 '''提示:''' 这个 PHP 文件 ("demo_test.php") 类似这样: == demo_test.php 文件代码: == <sample title="" desc="" lang="html" hererun="1"> <?php echo '这是个从PHP文件中读取的数据。'; ?> </sample> == jQuery $.post() 方法 == $.post() 方法通过 HTTP POST 请求向服务器提交数据。 '''语法:''' <pre>$.post(URL,callback); 或 $.post( URL [, data ] [, callback ] [, dataType ] )</pre> * '''URL''':发送请求的 URL字符串。 * '''data''':可选的,发送给服务器的字符串或 key/value 键值对。 * '''callback''':可选的,请求成功后执行的回调函数。 * '''dataType''':可选的,从服务器返回的数据类型。默认:智能猜测(可以是xml, json, script, 或 html)。 下面的例子使用 $.post() 连同请求一起发送数据: <sample title="" desc="" lang="html" hererun="1"> $("button").click(function(){ $.post("/try/ajax/demo_test_post.php", { name:"小白教程", url:"http://www.xiaobai.wang" }, function(data,status){ alert("数据: \n" + data + "\n状态: " + status); }); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $.post("/try/ajax/demo_test_post.php",{ name:"小白教程", url:"http://www.xiaobai.wang" }, function(data,status){ alert("数据: \n" + data + "\n状态: " + status); }); }); }); </script> </head> <body> <button>发送一个 HTTP POST 请求页面并获取返回内容</button> </body> </html> </run> $.post() 的第一个参数是我们希望请求的 URL ("demo_test_post.php")。 然后我们连同请求(name 和 url)一起发送数据。 "demo_test_post.php" 中的 PHP 脚本读取这些参数,对它们进行处理,然后返回结果。 第三个参数是回调函数。第一个回调参数存有被请求页面的内容,而第二个参数存有请求的状态。 '''提示:''' 这个 PHP 文件 ("demo_test_post.php") 类似这样: == demo_test_post.php 文件代码: == <sample title="" desc="" lang="html" hererun="1"> <?php $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; $url = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : ''; echo '网站名: ' . $name; echo "\n"; echo 'URL 地址: ' .$url; ?> </sample>
返回至“
JQuery – AJAX get() 和 post() 方法
”。
上一节:
JQuery_–_AJAX_load()_方法
下一节:
JQuery_noConflict()_方法