在jQuery中AJAX的写法有3种,$ajax,$post,$get这三种。其中$post和$get是简易写法,高层的实现,在调用他们的时候,会运行底层封装好的$ajax。
jquery怎么写ajax?
两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。
GET - 从指定的资源请求数据
POST - 向指定的资源提交要处理的数据
GET 基本上用于从服务器获得(取回)数据。注释:GET 方法可能返回缓存数据。
POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据。
get方式一$.get("test.cgi", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});
get方式二$.get("test.php", function(data){
alert("Data Loaded: " + data);
});
get方式三$.get("test.php", { name: "John", time: "2pm" } );
get的参数应该可以放在url上,还没试过。
post方式一$.post("test.php", { name: "John", time: "2pm" } );
post方式二$.post("test.php", $("#testform").serialize());
post方式三$.post("test.php", function(data){
alert("Data Loaded: " + data);
});
ajax写法$.ajax({
type: "POST",
dataType: "json",
url: "",
data: ""
success: function(data){
},
error: function(msg){
}
});
本文来自jQuery答疑栏目,欢迎学习!