我想你想要的東西的方法與ajaxSuccess()方法相反。它是ajaxSend() method。
$(document).ajaxSend(function (event, xhr, settings) {
console.log("ajaxSend");
});
$(document).ajaxSuccess(function (event, xhr, settings) {
console.log("ajaxSuccess");
});
$.post(url, function (result) {
console.log("post");
});
/*
console result
ajaxSend
post
ajaxSuccess
*/
補充:你可以這樣做,但我不建議後續代碼。只需選擇這個想法。
(function ($) {
$.post = function (url, data, callback, type) {
// shift arguments if data argument was omitted
if (jQuery.isFunction(data)) {
type = type || callback;
callback = data;
data = undefined;
}
return jQuery.ajax({
url: url,
type: "POST",
dataType: type,
data: data,
success: function (success, statusText, jqXHR) {
jQuery.event.trigger("ajaxCallbackBefore", [jqXHR, success]);
callback(success, statusText, jqXHR);
}
});
};
})(jQuery);
jQuery.fn.ajaxCallbackBefore = function (fn) {
return this.on("ajaxCallbackBefore", fn);
};
$(function() {
$(document).ajaxCallbackBefore(function() {
console.log("ajaxCallbackBefore");
});
$(document).ajaxSend(function() {
console.log("ajaxSend");
});
$(document).ajaxSuccess(function() {
console.log("ajaxSuccess");
});
$.post(url, function (result) {
console.log("post");
});
});
不知道你在問什麼。 – crush
簡單dennis .. –
xhr是首先被調用的ajax對象。 – crush