最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does jQuery.getJSON return no data? - Stack Overflow

programmeradmin1浏览0评论

I have this method:

function getUserName(guid) {
    var name = "Unbekannt";

    $.getJSON(urlCurrent, {
        "method" : "get_user_info",
        "guid" : guid,
        "auth_token" : temporaryAuthToken
    }, function(data) {
        if (data.status == 0) {
            alert(data.result[0].name);
            name = data.result[0].name;
        }
    });

    return name;
}

Nearly all is working: the Ajax-Request gets data and fires the callback-function, so that

alert(data.result[0].name);

shows a popup with this value: "Forename Surname"

But then at

return name;

the method returns "Unbekannt", although name should have the new value "Forename Surname". What happened and where´s the mistake?

Thx a lot

I have this method:

function getUserName(guid) {
    var name = "Unbekannt";

    $.getJSON(urlCurrent, {
        "method" : "get_user_info",
        "guid" : guid,
        "auth_token" : temporaryAuthToken
    }, function(data) {
        if (data.status == 0) {
            alert(data.result[0].name);
            name = data.result[0].name;
        }
    });

    return name;
}

Nearly all is working: the Ajax-Request gets data and fires the callback-function, so that

alert(data.result[0].name);

shows a popup with this value: "Forename Surname"

But then at

return name;

the method returns "Unbekannt", although name should have the new value "Forename Surname". What happened and where´s the mistake?

Thx a lot

Share Improve this question asked Aug 29, 2012 at 14:20 user1633440user1633440 1
  • 1 The call to getJSON is asynchronous. So, at the return name; the data probably won't have arrived yet. – Lee Taylor Commented Aug 29, 2012 at 14:22
Add a ment  | 

3 Answers 3

Reset to default 4

I would return the promise object from the function and act on it.

function getUserName(guid) {
    return $.getJSON(urlCurrent, {
        "method" : "get_user_info",
        "guid" : guid,
        "auth_token" : temporaryAuthToken
    });
}

getUserName(guid).done(function(data) {
    if (data.status == 0) {
        alert(data.result[0].name);
    }
});

And, if you wanted to do the status check up front, .then is good for that.

function getUserName(guid) {
    return $.getJSON(urlCurrent, {
        "method" : "get_user_info",
        "guid" : guid,
        "auth_token" : temporaryAuthToken
    }).then(function(data){
        return $.Deferred(function(def){
            if (data.status == 0) {
                return def.resolve(data);
            }
            return def.reject(data);
        });
    });
}

getUserName(guid).done(function(data) {
    alert(data.result[0].name);
});

When you return name it hasn’t been assigned the new value you got from the getJSON callback yet, since the ajax call is asynchronous and takes some time to plete.

You need to bring a callback function instead:

function getUserName(guid, callback) {
    var name = "Unbekannt";

    $.getJSON(urlCurrent, {
        "method" : "coinor.get_user_info",
        "guid" : guid,
        "auth_token" : temporaryAuthToken
    }, function(data) {
        if (data.status == 0) {
            alert(data.result[0].name);
            name = data.result[0].name;
            callback(name);
        }
    });
}

getUserName(guid, function(name) {
    alert(name);
});

Because return name; is performed before the ajax call has been returned.

If you want it to relate to the data that has been returned in the $.getJSON() call, it needs to be inside the callback function as well.

发布评论

评论列表(0)

  1. 暂无评论