-1

我发现这个问题几乎完全相同:Return value from nested function in Javascript

问题是该函数被传递给jQuery的$.ajax函数。这是我所拥有的:

function doSomething() {
    // Do some stuff here
    console.log(getCartInfo());
}

function getCartInfo() {
    var url = blog_location + '?reqCartData=1';

    $.ajax({
        url: url,
        type: 'get',
        success: function(data) {
            return data;  <------------- This
        }
    });
}

data如果有意义的话,我需要返回doSomething 函数。我试图返回整个$.ajax函数,但返回了整个对象。有任何想法吗?

4

2 回答 2

0

发送回调函数:

function getCartInfo(onSuccess) {
    var url = blog_location + '?reqCartData=1';

    $.ajax({
        url: url,
        type: 'get',
        success: function(data) {
            onSuccess(data);
        }
    });
}

getCartInfo(function(data) {
    console.log(data);
});
于 2013-03-08T12:19:41.080 回答
0

尝试这个

 function doSomething(date) {
    ........your data
}

function getCartInfo() {
    var url = blog_location + '?reqCartData=1';

    $.ajax({
        url: url,
        type: 'get',
        success: function(data) {
           doSomething(data);
        }
    });
}
于 2013-03-08T12:23:44.010 回答