Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX:等待响应?_Javascript_Ajax_Json - Fatal编程技术网

Javascript AJAX:等待响应?

Javascript AJAX:等待响应?,javascript,ajax,json,Javascript,Ajax,Json,我使用全局变量传递来自AJAX调用的响应: window.response = null; // most recent response from XMLHttpRequest // the callback function for XMLHttpRequest function userObjFromJSON() { if (this.readyState == 4) { var json = eval('(' + this.responseText + ')')

我使用全局变量传递来自AJAX调用的响应:

window.response = null; // most recent response from XMLHttpRequest

// the callback function for XMLHttpRequest
function userObjFromJSON() {
    if (this.readyState == 4) {
        var json = eval('(' + this.responseText + ')');
        window.response = json;
    }
    else {
        indicateLoading();
    }
}

// loads the info for this username on the page
function loadUsernameInfo(username) {
    clearPage();
    getUserInfo(username);
    var profile = window.response; // most recent json response (what if it hasn't come in yet?)
    window.response = null;
    if (profile) {
        indicateLoaded(username);
        fillInProfileInfo(profile);

        getTweets(username);
        var tweets = window.response; // most recent json response (what if it hasn't come in yet?)
        if (tweets) {
            fillInTweets(tweets, MAX_TWEETS);
            var mentions = mentionedUsers(tweets, MAX_TWEETS);
            fillInMentioned(mentions);
        }
        else {
            indicateUnavailableTweets();
        }
    }
    else {
        indicateInvalidUsername(username);
    }
}
问题是,当控制器函数想要开始填充信息时,AJAX调用并不总是返回。(如果我在调试器中缓慢地执行它,它工作得很好。)我能做些什么来解决这个问题

我试过这样的方法:

getUserInfo(username);
while (window.response == null); // infinite loop here
var profile = window.response; // most recent json response
但这只会让我的浏览器没有响应

我很犹豫是否从回调调用所需的函数,因为我正在尝试实现模型视图控制器。从模型中调用控制器/视图函数感觉像是打破了模式

function userObjFromJSON() {
if (this.readyState == 4) {
    var json = eval('(' + this.responseText + ')');
    window.response = json;
// why dont you call needed function here ?
}
else {
    indicateLoading();
}
}

设置window.response时,为什么不调用所有需要的函数


在最坏的情况下,您可以使用window.setTimeout等待ajax回复,但最好的方法是使用事件。

这里的最佳实践是将当前在
loadUsernameInfo
中的代码放入ajax调用本身的回调中,而不是依赖全局变量。这样,当您的响应返回时,执行的回调实际上会继续更新您的UI并执行任何其他相关任务,而不仅仅是设置window.response变量

另一种方法是从现有回调调用
loadUsernameInfo
,如:

// the callback function for XMLHttpRequest
function userObjFromJSON() {
    if (this.readyState == 4) {
        var profile = eval('(' + this.responseText + ')');
        loadUsernameInfo(username, profile);
    }
    else {
        indicateLoading();
    }
}

希望有帮助

您的XMLHttpRequest应该使用onreadystatechange事件。例如:

var xmlHttp=new XMLHttpRequest();
xmlHttp.onreadystatechange=function(){
    if( xmlHttp.readyState!=4 || (xmlHttp.status!=200 && xmlHttp.status!=304))return;
    callback(xmlHttp.responseText);

}

其中callback()是您希望它调用的函数。readyState为4表示内容已完成加载。这两个状态条目是为了确保url没有给出错误。

我正在尝试执行model view controller,而将函数调用放在那里会违反这一点。