【发布时间】:2013-04-02 20:27:43
【问题描述】:
我已经在 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);
},
})
});
这一次回调是按正确的顺序调用的。 有人可以澄清一下吗?
【问题讨论】:
-
这个问题实际上回答了我遇到的一个问题,所以虽然不太可能帮助别人,但确实如此。