从回调函数中返回JSON数据 [英] Returning JSON data out of the callback function

查看:317
本文介绍了从回调函数中返回JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从回调函数中返回以下javascript中的msg变量?我尝试执行此操作,但是即使在回调函数范围内msg变量中包含数据,也得到了空值.

Is it possible to return the msg variable in javascript below out of the callback function? I tried doing this, but got a null, even though the msg variable had data in it while in the scope of the callback function.

var msg = load();


function load()
{
     $.ajax({  
     type: "POST",  
     url: "myPage.aspx/MyMethod",  
     data: jsonText,  
     contentType: "application/json; charset=utf-8",  
     dataType: "json",  
     success: function (msg) { return msg;  // Doesn't return anything because
                                            //it's automatically called on success??? 
     },  
     failure: function () { alert("Failure"); }  
     }); 
}

推荐答案

不适用于异步请求,这是正常的ajax请求类型,也是首选类型.那是因为load在您从服务器获得答复之前就返回了,因此显然它不能返回msg.而是让load接受回调:

Not with an asynchronous request, which is the normal kind of ajax request and the preferred kind. That's because load returns before you get your reply from the server, so obviously it can't return the msg. Instead, have load accept a callback:

load(function(msg) {
    // Use msg here
});

function load(callback)
{
    $.ajax({  
        type: "POST",  
        url: "myPage.aspx/MyMethod",  
        data: jsonText,  
        contentType: "application/json; charset=utf-8",  
        dataType: "json",  
        success: function (msg) {
            // Call our callback with the message
            callback(msg);
        },  
        failure: function () {
            // Call our callback with an indication things failed
            callback(null); // Or however you want to flag failure
        }
     }); 
}

如果绝对不可避免,则可以通过在async: false来使用同步请求> $.ajax 选项(这是指向文档的链接),然后按以下步骤进行操作:

If absolutely unavoidable, you could use a synchronous request by setting async: false in your $.ajax options (that's a link to the docs), then do it like this:

var msg = load();

function load(callback)
{
    var result;
    $.ajax({  
        type: "POST",  
        url: "myPage.aspx/MyMethod",  
        data: jsonText,  
        contentType: "application/json; charset=utf-8",  
        dataType: "json",  
        async: false,
        success: function (msg) {
            // Set the `result` value here. We can't *return* it
            // here because, of course, that would just return it
            // from our `success` function.
            result = msg;
        },  
        failure: function () {
            result = null; // Or however you want to flag failure
        }
     });

     // Because we've made the request synchronous, we won't get here
     // until the ajax call is complete and `result` has been set by
     // the callbacks above; we can now return it.
     return result;
}

但是 同步 请求会带来糟糕的用户体验,此外,几乎从不需要,因此应尽可能避免.

But synchronous requests make for a poor user experience and, moreover, are almost never necessary, so should be avoided as much as possible.

这篇关于从回调函数中返回JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆