Javascript函数AJAX调用返回undefined

时间:2016-08-21 18:52:51

标签: javascript jquery ajax

当进行AJAX调用时,onsuccess不会返回字符串构建器数据,而是返回未定义。

var MyApp = window.MyApp || {};

MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function() {
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: ShowMethodSucceeded,
            error: FailedMethod
        });
    },
    ShowMethodSucceeded = function(data) {
        var results = data.d.results;
        if(results.length > 0) {
            temp = '';
            for(var i = 0; i < results.length; i++) {
               temp += 'string builder stuff';
            }
        } else {
            alert('no records');
        }
        return temp;
    },
    FailedMethod = function() {
        alert('error');
    };
    return {
        Show: ShowMethod
    };
}();

$(document).ready(function() {
   $('#divResults').append(MyApp.Data.Show());
});

此外,这里有几个不同版本的代码没有收到正确的结果。

MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function() {
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: ShowMethodSucceeded,
            error: FailedMethod
        });
    },
    return temp;
    // returns 'Test' instead of the string builder

MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function() {
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: function(data) { temp = data; },
            error: FailedMethod
        });
    },
    return temp;
    // returns 'Test' instead of the JSON from data

如何从AJAX调用的成功函数返回数据?

在进行AJAX调用时,函数已完成执行,并且尚未填充返回变量。 UI是动态构建的,我想使用字符串构建器函数返回字符串以附加动态构建UI的函数。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

我认为你需要在整个过程中进行回调,如下:

MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function(cb) { // added cb
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: function (data) {
                ShowMethodSucceeded(data, cb); // pass cb through
            },
            error: FailedMethod
        });
    },
    ShowMethodSucceeded = function(data, cb) { // added cb
        var results = data.d.results;
        if(results.length > 0) {
            temp = '';
            for(var i = 0; i < results.length; i++) {
               temp += 'string builder stuff';
            }
        } else {
            alert('no records');
        }
        cb(temp); // call cb with the result
    },
    FailedMethod = function() {
        alert('error');
    };
    return {
        Show: ShowMethod
    };
}();

$(document).ready(function() {
    MyApp.Data.Show(function (result) { // pass a callback
        $('#divResults').append(result); // use the result
    });
});