js ajax回调 return,JS : Return result from nested ajax success functi

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

How do I return the result of the nested ajax call as the result of the parent function?

//Declaring the function

var myFunction = function(myData){

$.ajax({

type:'post',

url:"/ajaxPage.php",

data:{data:myData},

success:function(r){

return r;

});

}

//Calling the function

var theResult = myFunction(myData);

I want the variable 'theResult' to hold the contents of the ajax call.

回答1:

You will have to make your AJAX call synchronous (not asynchronous which is the default).

Something like this:

//Declaring the function

var myFunction = function(myData){

var returnValue = null;

$.ajax({

type:'post',

async: false,

url:"/ajaxPage.php",

data:{data:myData},

success:function(r){

returnValue = r;

});

return returnValue;

}

//Calling the function

var theResult = myFunction(myData);

回答2:

Since the ajax is asynchronous you cannot return it in the parent function.

What you can do, is to provide a callback function, and you call it as well with the result.

//Declaring the function

var myFunction = function(myData, callback){

$.ajax({

type:'post',

url:"/ajaxPage.php",

data:{data:myData},

success:function(r){

callback(r);

});

}

//Calling the function

var theResult = myFunction(myData, function(res) {

// deal with it..

});

回答3:

If you're using jQuery, you should definitely be using $.Deferred() and Promises/A. I've made this more verbose that you really need to demonstrate some of the functionality. $.ajax itself is already returning a $.Deferred().promise() so you can actually just cut out the extra step if you only need to make the one XHR request (see bottom example).

//Declaring the function

var myFunction = function(myData){

var deferredResponse = new $.Deferred();

$.ajax({

type: 'post',

url: '/ajaxPage.php',

data: {'data': myData}

}).done(deferredResponse.resolve).fail(deferredResponse.reject);

return deferredResponse.promise();

}

//Calling the function

$.when(myFunction(myData)).done(function(response){

var theResult = response;

});

The verbose syntax comes in handy when you have multiple nested XHR requests and want to return the response of the inner-most call: deferredResponse.resolve(innerResponse). Here's the simply version.

//Declaring the function

var myFunction = function(myData){

return $.ajax({

type: 'post',

url: '/ajaxPage.php',

data: {'data': myData}

});

}

//Calling the function

myFunction(myData).done(function(response){

var theResult = response;

});

回答4:

Try this:

var myFunction = function(myData){

$.ajax({

type:'post',

url:"/ajaxPage.php",

data:{data:myData},

success:function(r){

return arguments.callee(r);

});

}

//Calling the function

var theResult = myFunction(myData);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值