How to override jquery.ajax function?

I want to override the $ .ajax function in jQuery

I want to use existing functionality and add some other features

Example:

get the original ajax function in the 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 advise how to implement it?

+4
source share
2 answers

Yes you can do it ...

 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 like ajaxComplete() , where you can check the status of the response.

+13
source

You can simply override the default behavior of $ .ajax.

Remember that $ .ajax returns prom () objects, so you will need to handle callbacks associated with done () / fail ().

Perhaps this article may help you: http://www.svlada.com/blog/override-jquery-ajax-handler/

0
source

Source: https://habr.com/ru/post/1436286/


All Articles