問題:在Chrome中工作時,腳本未等待同步xhr響應。在FireFox/IE中正常工作。已經使用onreadyStateHandlers,但仍然在收到響應之前執行其他代碼。XHR同步調用在Chrome中失敗。不等待xhr響應的腳本
function callingScript()
{ var a=callServer();
alert(a);//Here a is showing undefined while executing in chrome.
But in FF/IE its waiting for the response and then alerts a with response.
}
function callServer()
{
var response;
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST","abc",false);
httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
httpRequest.onreadystatechange = function()
{
if (httpRequest.readyState != 4)
{return; }
if (httpRequest.status != 200)
{response=httpRequest.status;}
else {response=httpRequest.responseText;}
};
httpRequest.send(data);
return response;
}
請分享您的想法!
被告知:說你的問題是緊迫的是不會幫助你得到答案,可實際上把人們遠離幫助您。 –