gpt4 book ai didi

javascript - 如何通过 JQuery ajaxSend 事件覆盖成功函数

转载 作者:搜寻专家 更新时间:2023-11-01 04:10:27 24 4
gpt4 key购买 nike

我试图在 ajaxsend 事件上覆盖成功函数,但它不起作用这是代码:

    $(document).ajaxSend(function(event,xhr,options){
console.log('ajaxSend');
var tempSuccess = options.success;
options.success = function(data, textStatus, jqXHR){
console.log('start');
tempSuccess(data, textStatus, jqXHR);
console.log('end');
}; xhr.success = options.success;});

在 AJAX 上我确实在控制台中看到“ajax”,但成功后我看不到开始和结束调试消息..

我做错了什么?

最佳答案

ajaxSend 无法完成您想要完成的任务。问题是 ajaxSend 显然与原始 xhroptions 对象的副本一起工作,因此修改不会有任何效果。您可以使用以下代码轻松地对此进行测试:

$(document).ajaxSend(function(event, xhr, options){
delete options.success;
console.log(options.success); // undefined
});
$.ajax({
url: "test.html",
success: function() { console.log("this will be printed nevertheless"); }
});


所以你不能使用 ajaxSend 来覆盖成功回调。相反,您将不得不“破解”jQuery 的 AJAX 函数:

// closure to prevent global access to this stuff
(function(){
// creates a new callback function that also executes the original callback
var SuccessCallback = function(origCallback){
return function(data, textStatus, jqXHR) {
console.log("start");
if (typeof origCallback === "function") {
origCallback(data, textStatus, jqXHR);
}
console.log("end");
};
};

// store the original AJAX function in a variable before overwriting it
var jqAjax = $.ajax;
$.ajax = function(settings){
// override the callback function, then execute the original AJAX function
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 ,所以这应该适用于通过 jQuery 完成的每个 AJAX 请求(虽然我还没有测试过......)。



通过破解 XMLHttpRequest.prototype,类似的东西也应该与“纯”JavaScript 一起工作。请注意,以下内容在 IE 中不起作用,它使用 ActiveXObject 而不是 XMLHttpRequest

(function(){
// overwrite the "send" method, but keep the original implementation in a variable
var origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data){
// check if onreadystatechange property is set (which is used for callbacks)
if (typeof this.onreadystatechange === "function") {
// overwrite callback function
var origOnreadystatechange = this.onreadystatechange;
this.onreadystatechange = function(){
if (this.readyState === 4) {
console.log("start");
}
origOnreadystatechange();
if (this.readyState === 4) {
console.log("end");
}
};
}
// execute the original "send" method
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();

关于javascript - 如何通过 JQuery ajaxSend 事件覆盖成功函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12003978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com