Ajax四步操作

第一步得到(XMLHttpRequest)
function creatXMLHttpRequest(){
try{
return new XMLHttpRequest();
} catch(e){
try{
return new ActiveXObject(Msxml2.XMLHTTP);
}catch{
try{
return new ActiveXObject(Microsoft.XMLHTTP);
}catch(e){
alert("你的浏览器不支持AJax")
throw e;
}
}
}

第二步打开与服务器连接
var xmlHttp = creatXMLHttpRequest();
xmlHttp.open();
用来建立与服务器的连接有三个参数
请求方式GET或POST
url:指定服务器资源
第三个参数如果为true则异步
第三步发送请求
xmlHttp.send(null):如果不给可能会造成部份浏览器无法发送!
> 参数:就是请求体内容!如果是GET请求,必须给出null。

第四步
获取请求状态
xmlHttp.onreadystatuschange="myfunction";
得到xmlHttp的状态
var state = xmlHttp.readyState();
得到服务器的响应状态吗
ar status = xmlHttp.status();
*得到服务器的响应内容
var content = xmlHttp.response.Texxt;//文本格式内容
var content = xmlHttp.response.XML;//xml的内容
function myfunction(){
if(state == 4&&status==200){

}
}

原文地址:https://www.cnblogs.com/code-changeworld/p/4117813.html