一、解决了什么问题?
- 在不刷新网页的情况下去更新数据
二、核心:
1.兼容写法:
If(window.ActiveObject){
Ajax=new ActiveXObject(“Microsoft.XMLHTTP”);
}else if(window.XMLHttpRequest){
Ajax=new XMLHttpRequest( );
}
2.IE6不兼容
三、用法
1.创建
Var ajax=new XMLHttpRequest( )
2.打开
同步:
Ajax.open(“get”,”xxx.php”,false):参数2 服务器地址 参数3 是否异步
Ajax.open(“post”,xxx.php,false):
Ajax.setRequestHeader(“content-Type”,”application/x-www-form-urlencoded)
异步:
Ajax.open(“get”,”xxx.php”,true)
Ajax.open(“get”,”xxx.php”,true):
Ajax.setRequestHeader(”application/x-www-form-urlencoded)
发送后状态值readystate
0 对象已建立,还未初始化,未调用send
1 open已调用,未调用 send
2 send已调用,其他未知
3 请求已发送,正在接收数据
4 表示数据已经收到
3.监听
Ajax.onreadystatechange=function( ){
Console.log(ajax.responseText)
}
4.发送
Ajax.send(null) 发送后返回四种参数
responseText 作为响应的主体返回的文本
responseXML
Status 200成功
202请求接受,处理未完成
400语法错误
404找不到指定url
500内部服务器错误
statusText 跨浏览器时可能不太一样