jQuery的get()和post()方法
get()和post()方法
通过HTTP的GET或POST请求,从服务器请求数据
GET
从指定的资源,请求数据
从服务器获得取回数据,可能返回缓存数据
POST
向指定的资源,提交要处理的数据
从服务器获取数据,不会缓存数据,常用于连同请求,一起发送数据
get()
通过HTTP的GET请求,从服务器上请求数据
语法格式
$.get(URL,callback);
参数说明
URL
必选参数,请求的URL
Callback
可选参数,执行的回调函数
$("button").click(function(){
$.get("demo_test.php",function(data,status){
alert("数据: " + data + "\n状态: " + status);
});
});
post()
通过HTTPPOST请求,向服务器提交数据
语法格式
$.post(URL,data,callback);
参数说明
URL
必选参数,请求的URL
Data
可选参数,请求参数
连同请求发送的数据,JSON串,key/value格式
Callback
可选参数,回调函数
请求成功之后,执行的函数
$("button").click(function(){
$.post("/try/ajax/demo_test_post.php",
{
name:"教程",
url:"http://www.runoob.com"
},
function(data,status){
alert("数据: \n" + data + "\n状态: " + status);
});
});