1、原生ajax
1、实例化xhr
var xhr=new XMLHttpRequest
2、建立连接
xhr.open(请求方式,请求地址)
3、设置请求体
xhr.requestHeader('Contet-Type','application/x-www-form-urlencoded')//查询字符串
xhr.requestHeader('Contet-Type','application/json')//json字符串
查询字符串
key1=val1&key2=val2&......
json字符串
’{'name':tom
对象序列化
4、发送请求
xhr.send([data])
5、监听响应
xhr.onreadystatechange=function(){
//请求是否成功发送
if(xhr.readystate==4){
//是否成功拿到响应结果
if (xhr.states==200){
}
}
}
2、jQuery中的ajax
ajax({
url:' ',
method:' ',
contentType:'application/json ' //只有参数是json字符串的时候才需要设置,默认参数是查询字符串
[data]:obj //查询字符串
[data]:JSON.stringify(obj) //json字符串
success:function(res){}
error:function(){}
})