3

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

しかし、 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 !");
        }
    }
}

そして、それを次のように呼び出します

xmlHttp.onreadystatechange = handleServerResponse();

うまくいきません。間違っていたら指摘してください。

4

4 に答える 4

7

使ってみて

xmlHttp.onreadystatechange = handleServerResponse;

削除された括弧に注意してください。

于 2013-10-19T06:54:50.770 に答える
4

2つのこと:

xmlHttp.open("GET", "hello.txt", true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();

openご覧のとおり、括弧を削除し、との順序を逆にしましたonreadystatechange

まず第一に、それ以外の場合は、関数参照自体を関連付けるのではなく、関数の戻り値を関連付けるためです。基本的に、関数を実行しているためです。持っているのと同じ違いです:

var a = sum(1, 2); // 3, assign to `a` the return value of `sum`
var b = sum; // assign to `b` the `sum` function ref.
var c = b(1, 2); // 3, therefore `b` is an 'alias' to `sum` 

2 つ目は、ブラウザによって異なります。たとえば、メソッドが呼び出されるたびに、IE の一部のバージョンはインスタンスの を「リセット」onreadystatechangeします。そのため、「イニシャライザ」として機能する の前にを設定すると、ブラウザによっては、メソッドが呼び出されると削除される可能性があるため、呼び出されない可能性があります。したがって、完全に互換性を持たせるには、 メソッドの後に を設定することをお勧めしますが、もちろん の前に.XMLHttpRequestopenonreadystatechangeopenopenonreadystatechangeopensend

于 2013-10-19T07:03:21.617 に答える
1

これを使って xmlHttp.onreadystatechange = handleServerResponse

次に、関数 handleServerResponse(xmlHttp) として記述します。

于 2013-10-19T07:15:04.690 に答える