I want to override the $.ajax function in Jquery
I want to use the existing functionality and need to add some other functionality
Example:
get the original ajax function in to OriginalAjax variable
var OriginalAjax = $.ajax
$.ajax = function(xhr){
if(xhr.status ==401)
{
// Diplay Error in dialog box;
}
return(OriginalAjax(xhr));
});
is it possible to override the $.ajax function? if 'Yes' Please let know how to implement it?
The jqXHR Object. The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.
jQuery - AJAX get() and post() Methods. The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request.
dataType The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response. "text": A plain text string. So you want contentType to be application/json and dataType to be text : $.
Yes, you can do that...
var oldAjax = $.ajax;
$.ajax = function() {
// Do your magic.
return oldAjax.apply($, arguments);
};
...or...
const oldAjax = $.ajax;
$.ajax = (...args) => {
// Do your magic.
return oldAjax(...args);
};
However, judging by your example, you should just use a global event handler such as ajaxComplete()
, where you can check the response status.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With