在jQuery中AJAX的写法有3种,
a
j
a
x
,
ajax,
ajax,post,
g
e
t
这
三
种
。
其
中
get这三种。其中
get这三种。其中post和
g
e
t
是
简
易
写
法
,
高
层
的
实
现
,
在
调
用
他
们
的
时
候
,
会
运
行
底
层
封
装
好
的
get是简易写法,高层的实现,在调用他们的时候,会运行底层封装好的
get是简易写法,高层的实现,在调用他们的时候,会运行底层封装好的ajax。
ajax案例
$.ajax({
url:"http://www.microsoft.com", //请求的url地址
dataType:"json", //返回格式为json
async:true,//请求是否异步,默认为异步,这也是ajax重要特性
data:{"id":"value"}, //参数值
type:"GET", //请求方式
beforeSend:function(data){
//请求前的处理
},
success:function(req){
//请求成功时处理
},
complete:function(data){
//请求完成的处理
},
error:function(data){
//请求出错处理
}
});
get请求
$.get("url",{id:1},function(data){
})
post请求
$.post("url",{id:1},function(data){
})
请求参数也可以直接写在ajax请求中
$(function(){
//请求参数
var list = {};
//
$.ajax({
//请求方式
type : "POST",
//请求的媒体类型
contentType: "application/json;charset=UTF-8",
//请求地址
url : "http://127.0.0.1/admin/list/",
//数据,json字符串
data : JSON.stringify(list),
//请求成功
success : function(result) {
console.log(result);
},
//请求失败,包含具体的错误信息
error : function(e){
console.log(e.status);
console.log(e.responseText);
}
});
});