Ajax

2017-07-03  本文已影响0人  _Enco_

创建Ajax对象

var xhr = null;
if(window.XMLHttpRequest){
  xhr = new XMLHttpRequest(); // 标准浏览器
}else{
  xhr = new ActiveXObject('Microsoft.XMLHTTP'); //早起IE浏览器
}

准备发送请求(配置发送请求的一些行为)

xhr.open(' get ', ' form.php ', true);  // 其中中间参数为发送到的文件地址

执行发送的动作

xhr.send(null);

指定回调函数

xhr.onreadystatechange = function(){
  if(xhr.readyState == 4){
    if(xhr.status == 200){
      var data = xhr.responseText; // json
      // var data2 = xhr.responseXML;
        }    
    }
 }

xhr.readyState

上一篇 下一篇

猜你喜欢

热点阅读