क्या आप पूरा करने के लिए कोशिश कर रहे हैं ajaxSend
के साथ नहीं किया जा सकता है। समस्या यह है कि ajaxSend
स्पष्ट रूप से मूल xhr
और options
ऑब्जेक्ट्स की एक प्रति के साथ काम करता है, इसलिए संशोधनों का कोई प्रभाव नहीं पड़ेगा। आप आसानी से निम्नलिखित कोड के साथ इस परीक्षण कर सकते हैं:
$(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
उपयोग नहीं कर सकते सफलता कॉलबैक अधिलेखित करने के लिए।
// 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
हमेशा की तरह उपयोग कर सकते हैं: जहाँ तक मुझे पता, jQuery के 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
हैकिंग से "शुद्ध" जावास्क्रिप्ट के साथ काम करना चाहिए। ध्यान दें कि निम्नलिखित IE में काम नहीं करेगा, जो
XMLHttpRequest
के बजाय
ActiveXObject
का उपयोग करता है।
(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();
संबंधित: http://stackoverflow.com/questions/293668/jquery-ajax-prevent-callback-from-running – pimvdb
धन्यवाद, लेकिन यह मेरे प्रश्न का उत्तर नहीं देता है, AJAX को HTTP प्रतिक्रिया प्राप्त करने के बाद पूर्ण आग लगती है, मैं AJAXSend के बारे में पूछ रहा हूं जो HTTP अनुरोध – ciochPep
से पहले ठीक है। आप सही हैं। मैं केवल उस जवाब पर इशारा कर रहा था जो कहता है कि कॉलबैक चलाने के लिए बाध्य हैं और आप इसे रोक नहीं सकते हैं। यह सच है कि यह वास्तव में आपका प्रश्न नहीं है; मुझे एक समाधान में दिलचस्पी है। – pimvdb