如何管理调用Ajax调用
问题描述:
内部功能齐全,我想提出的订购3 Ajax调用应执行一个又一个如何管理调用Ajax调用
我能够对此进行管理 第一和第二Ajax调用?
我的问题是我怎样才能调用第三个Ajax调用基于第二个完成?
$(document).ready(function() {
// First Ajax call
var locationAjaxCall = $.ajax({
type: 'GET',
url: url + '/OMS/oms1/getlocationscreen?screen_ids=' + screen_ids,
jsonpCallback: 'jsonCallback',
cache: true,
dataType: 'jsonp',
jsonp: false,
success: function(response) {
var resp = JSON.stringify(response);
alert(resp);
},
error: function(xhr, ajaxOptions, thrownError) {
}
});
// Second Ajax call
locationAjaxCall.done(function() {
$.ajax({
type: 'GET',
url: url + '/OMS/oms1/fromscratchmodified?screen_ids=' + screen_ids,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
jsonp: false,
success: function(response) {
}
});
});
// Third Ajax call
$.ajax({
type: 'GET',
url: url + '/OMS/oms1/fetchcustomerid?UUID=' + qruuid,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
jsonp: false,
success: function(response) {
}
});
});
答
尝试使用Ajax调用的回调.done()
如下图所示: -
$(document).ready(function() {
$.ajax({
// First Ajax call
}).done(function() {
$.ajax({
// Second Ajax call
}).done(function() {
// Third Ajax call
});
});
});
答
可以使用done
(或then
,如果你想处理故障以及)函数上所有的人:
$.ajax({/*...first call...*/
}).done(function() {
$.ajax({/*...second call...*/
}).done(function() {
$.ajax({/*...third call...*/});
});
});
...如果它真的很重要,他们做一个之后。
我不认为这是你在问什么,但如果你只需要三个完成你移动到下一个事情之前,你可以使用$.when
$.when(
$.ajax({/*...first call...*/}),
$.ajax({/*...second call...*/}),
$.ajax({/*...third call...*/})
).then(function() {
// Do the next thing
});
的调用将并行运行,并在最后一个调用完成后调用回调。
非常感谢你非常。完美工作。 – Kiran 2014-12-04 10:31:41
@Kiran ...你是受欢迎的.. !!!! – 2014-12-04 10:32:01