What you are trying to accomplish cannot be accomplished with ajaxSend. The problem is that it ajaxSendapparently works with a copy of the original objects xhrand options, therefore, the changes will have no effect. You can easily verify this with the following code:
$(document).ajaxSend(function(event, xhr, options){
delete options.success;
console.log(options.success);
});
$.ajax({
url: "test.html",
success: function() { console.log("this will be printed nevertheless"); }
});
ajaxSend . "" jQuery AJAX:
(function(){
var SuccessCallback = function(origCallback){
return function(data, textStatus, jqXHR) {
console.log("start");
if (typeof origCallback === "function") {
origCallback(data, textStatus, jqXHR);
}
console.log("end");
};
};
var jqAjax = $.ajax;
$.ajax = function(settings){
settings.success = new SuccessCallback(settings.success);
jqAjax(settings);
};
})();
$.ajax, :
$.ajax({
url: "test.html",
success: function() {
console.log("will be printed between 'start' and 'end'");
}
});
, jQuery AJAX (, $.get() .load()) $.ajax, AJAX, jQuery ( , ...).
- "" JavaScript,
XMLHttpRequest.prototype. , IE :
XMLHttpRequest ActiveXObject.
(function(){
var origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data){
if (typeof this.onreadystatechange === "function") {
var origOnreadystatechange = this.onreadystatechange;
this.onreadystatechange = function(){
if (this.readyState === 4) {
console.log("start");
}
origOnreadystatechange();
if (this.readyState === 4) {
console.log("end");
}
};
}
origSend.call(this, data);
};
})();
( XMLHttpRequest):
var xhr = new XMLHttpRequest();
xhr.open("POST", "test.html", true);
xhr.onreadystatechange = function(){
if (xhr.readyState === 4) {
console.log("will be printed between 'start' and 'end'");
}
};
xhr.send();