JQuery ajax返回其他函数的值

时间:2012-06-13 12:05:50

标签: javascript jquery

你好我有下一个函数,结果总是返回null,但服务调用成功分支有任何错误, 我想修复它:1。删除异步属性,2。使GetStore函数调用的其他函数应该处理该函数的结果。我怎么能以正确的方式做到这一点? ()

感谢。

 function GetStore() {
        $.ajax({
            url: serviceurl + 'GetSometing',
            type: 'POST',
            contentType: 'application/json',
            dataType: "json",
            async: false,
            success: function (result) {
                return result;
            },
            error: function (xhr, description, error) {
                return null;
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

如果你想让它异步,你不能等它返回一个值,你必须实现回调函数。

function GetStore(success, error) {
    $.ajax({
        url: serviceurl + 'GetSometing',
        type: 'POST',
        contentType: 'application/json',
        dataType: "json",
        success: success,
        error: error
    });
}

GetStore(function(result) {
    // successful result
}, function(xhr, description, error) {
    // an error occurred
});