在我的Javascript文件中,我有多个ajax调用,我想将所有ajax调用更改为一个ajax调用.这是我的示例JS代码.
var vpnPin = $.ajax({
dataType: "json",
url: "url",
async: true,
success: function(result) {}
}); //$.ajax() ends
var vpnDataPlan = $.ajax({
dataType: "json",
url: "url",
async: true,
success: function(result) {}
}); //$.ajax() ends
Run Code Online (Sandbox Code Playgroud)
我们怎样才能将多个AJAX调用调用到一个AJAX调用?
可以在javascript中使用promises概念.阅读承诺并推迟.
使用上面的示例可以像下面这样完成:
//assuming vpnPin and vpnDataPlan are the deferred object returned from $.ajax.
//As you have done in your example.
$.when(vpnPin, vpnDataPlan).done(function(){
//work to do after the completion of both calls.
});
Run Code Online (Sandbox Code Playgroud)
还有更多这方面的变化.RSVP实现了处理承诺的标准方式.
| 归档时间: |
|
| 查看次数: |
6792 次 |
| 最近记录: |