2

2 つの無関係な機能がサーバーにリクエストを行っているリクエスト システムがあります。しかし、問題は応答が正しくないことです。何が起こっているのかを段階的に説明しましょう。

A background function makes a request to the server
Server processes task 1
A second unrelated background function makes a request to the server
Client recieves response of task 1
The second function recieves that response that was for the first function. 
The first function never gets a response.

今はそれを解決する方法がわかりませんが、ここで競合がないように、それらを個別に区別する必要があることはわかっています。

これは、リクエストのものを処理する私の現在のコードです:

function call_back(result,func){
        if(typeof(func) != 'undefined' || func != false){       
            func(result);
        } else {
            return false;
        }
}

function caller(url,cfunc){
        if (window.XMLHttpRequest)
          {
            xmlhttp=new XMLHttpRequest();
          }
        else
          {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
            xmlhttp.onreadystatechange=cfunc;
            xmlhttp.open("GET",url,true);
            xmlhttp.send();
}

function call_file(url,func){ //handles html files (not json_encoded)
    caller(url,function(){
        if ( xmlhttp.readyState== 4 && xmlhttp.status== 200 ){
            call_back(xmlhttp.responseText,func);
        }
    });
}

function call_data(url,func,JsonBool){ //handles json_encoded data 
    caller(url,function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
        call_back(JSON.parse(xmlhttp.responseText),func);               
        }
    });                                           
}

この動作を防ぐために、関数に対して何ができますか?

4

2 に答える 2

0

It looks to me as though you're using a global/window variable for xmlhttp. If this is the case, certain parts of the second call will overwrite the first. Consider using an Object Oriented approach, or otherwise instantiating these as vars in different scopes.

于 2013-07-22T03:43:22.450 に答える