我在 jQuery 1.7 中对此进行了编码:
$.when($.ajax({
type: "GET",
url: internalOrderServiceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: $.proxy(this.retrieveInternalOrderSuccess, this),
error: $.proxy(this.retrieveInternalOrderError, this)
}), $.ajax({
type: "GET",
url: rejectionReasonServiceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: $.proxy(this.retrieveRejectionReasonSuccess, this),
error: $.proxy(this.retrieveRejectionReasonError, this)
})
).done(
$.ajax({
type: "GET",
url: salesOrderInfoServiceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: $.proxy(this.retrieveServiceItemSuccess, this),
error: $.proxy(this.retrieveServiceItemError, this)
})
);
然而回调retrieveServiceItemSuccess 在retrieveInternalOrderSuccess 和retrieveRejectionReasonSuccess 之前执行。谁能告诉我这有什么问题?
我已将代码更改为:
$.when($.ajax({
type : "GET",
url : internalOrderServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveInternalOrderSuccess, this),
error : $.proxy(this.retrieveInternalOrderError, this)
}), $.ajax({
type : "GET",
url : rejectionReasonServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveRejectionReasonSuccess, this),
error : $.proxy(this.retrieveRejectionReasonError, this)
})).done(function() {
$.ajax({
type : "GET",
url : salesOrderInfoServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveServiceItemSuccess, this),
error : $.proxy(this.retrieveServiceItemError, this)
})
});
但是这一次,第一个回调 retrieveInternalOrderSuccess 执行然后第二个回调执行 (retrieveRejectionReasonSuccess) - 这两个回调的执行顺序是随机的。但是,第三个回调不执行。有人可以建议有什么问题吗?
我试图添加这个:
var self = this;
$.when($.ajax({
type : "GET",
url : internalOrderServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveInternalOrderSuccess, this),
error : $.proxy(this.retrieveInternalOrderError, this)
}), $.ajax({
type : "GET",
url : rejectionReasonServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveRejectionReasonSuccess, this),
error : $.proxy(this.retrieveRejectionReasonError, this)
})).done(function() {
$.ajax({
type : "GET",
url : salesOrderInfoServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(oResult) {
self.retrieveServiceItemSuccess(oResult);
},
error : function(oResult) {
self.retrieveServiceItemError(oResult);
},
})
});
这次回调以正确的顺序调用。任何人都可以澄清这一点吗?