xmlhttp的onreadystatechange注意点

错误示范:

//发送get请求
    function getInnerCode(outterCode){
        xmlHttp = new XMLHttpRequest();
        var url = "http://192.168.15.70:8081/getDfpInfo?originStr=" + outterCode;  
        xmlHttp.open("GET", url, true);// 异步处理返回  
//区别在这里!!!!!
        xmlHttp.onreadystatechange = getOkGet();
        xmlHttp.send(null);   
    }

 function getOkGet(){ 
        if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){ 
                console.log(xmlHttp.readyState); 
          } 
          if (xmlHttp.readyState==4 && xmlHttp.status==200){ 
              var d = xmlHttp.responseText; 
                console.log(d);
          } 
        console.log(xmlHttp);
        } 

正确示范:

//发送get请求
    function getInnerCode(outterCode){
        xmlHttp = new XMLHttpRequest();
        var url = "http://192.168.15.70:8081/getDfpInfo?originStr=" + outterCode;  
        xmlHttp.open("GET", url, true);// 异步处理返回  
//区别在这里!!!!!
        xmlHttp.onreadystatechange = function(){
            getOkGet();
        }
        xmlHttp.send(null);   
    }

 function getOkGet(){ 
        if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){ 
                console.log(xmlHttp.readyState); 
          } 
          if (xmlHttp.readyState==4 && xmlHttp.status==200){ 
              var d = xmlHttp.responseText; 
                console.log(d);
          } 
        console.log(xmlHttp);
        } 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容