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);
        } 

你可能感兴趣的:(xmlhttp的onreadystatechange注意点)