onreadystatechange函数在AJAX中不起作用

Abd*_*lik 3 javascript ajax

就XMLHttpRequest()对象而言,它很好,问题在于onreadystatechange,例如,如果我以这种方式放置代码,它就完美了.

function process(){
    var xmlHttp = new XMLHttpRequest();

    if(xmlHttp){
        xmlHttp.onreadystatechange = function(){
            theD = document.getElementById("theD");
            if(xmlHttp.readyState == 1){
                theD.innerHTML += "Status 1: Server connection established ! <br/>";
                }
            else if(xmlHttp.readyState == 2){
                theD.innerHTML += "Status 2: Request recieved ! <br/>";
                }
            else if(xmlHttp.readyState == 3){
                theD.innerHTML += "Status 3: Processing Request ! <br/>";
                }
            else if(xmlHttp.readyState == 4){

                if(xmlHttp.status == 200){
                    var text = xmlHttp.responseText;
                    theD.innerHTML += "Status 4: Processing Request ! <br/>";
                    theD.innerHTML += text;
                    }
                else{
                    alert("Something is wrong !");
                    }
                }
            };
        xmlHttp.open("GET", "hello.txt", true);
            xmlHttp.send();
        }
}
Run Code Online (Sandbox Code Playgroud)

但如果我创建handleServerResponse()的函数

function handleServerResponse(){
    theD = document.getElementById("theD");
            if(xmlHttp.readyState == 1){
                theD.innerHTML += "Status 1: Server connection established ! <br/>";
                }
            else if(xmlHttp.readyState == 2){
                theD.innerHTML += "Status 2: Request recieved ! <br/>";
                }
            else if(xmlHttp.readyState == 3){
                theD.innerHTML += "Status 3: Processing Request ! <br/>";
                }
            else if(xmlHttp.readyState == 4){

                if(xmlHttp.status == 200){
                    var text = xmlHttp.responseText;
                    theD.innerHTML += "Status 4: Processing Request ! <br/>";
                    theD.innerHTML += text;
                    }
                else{
                    alert("Something is wrong !");
                    }

                }
}
Run Code Online (Sandbox Code Playgroud)

并称之为

xmlHttp.onreadystatechange = handleServerResponse();
Run Code Online (Sandbox Code Playgroud)

它不起作用.如果我错了,请指出.

The*_*ode 7

尝试使用

xmlHttp.onreadystatechange = handleServerResponse;

请注意删除的paranthesis.


ZER*_*ER0 5

两件事情:

\n\n
xmlHttp.open("GET", "hello.txt", true);\nxmlHttp.onreadystatechange = handleServerResponse;\nxmlHttp.send();\n
Run Code Online (Sandbox Code Playgroud)\n\n

open如您所见,我删除了括号并颠倒了和 的顺序onreadystatechange

\n\n

首先,因为否则您不会关联函数引用本身,而是关联函数的返回值 \xe2\x80\x93 ,因为基本上,您正在执行它。这是相同的区别:

\n\n
var a = sum(1, 2); // 3, assign to `a` the return value of `sum`\nvar b = sum; // assign to `b` the `sum` function ref.\nvar c = b(1, 2); // 3, therefore `b` is an \'alias\' to `sum` \n
Run Code Online (Sandbox Code Playgroud)\n\n

第二件事,这取决于浏览器:例如,某些版本的 IE 在每次调用该方法onreadystatechange时都会“重置”实例。因此,如果您在之前设置 , 作为“初始化器”,则有可能(取决于浏览器)将删除 \xe2\x80\x93 ,因此一旦调用该方法就永远不会调用 \xe2\x80\ x93 .\n因此为了完全兼容,最好在方法 \xe2\x80\x93之后设置,当然在.XMLHttpRequestopenonreadystatechangeopenopenonreadystatechangeopensend

\n