How to prevent overwriting jQuery $ .ajaxSetup () parameters?

I have a question regarding ajaxSetup . When the script starts, I define the following:

 $.ajaxSetup({ success: function(data) { example(); } }); 

So far, everything is working fine. The problem starts with using Ajaxify, which overwrites the success function.

How can I prevent this? Is it possible to perform both functions or add the installation success function to the one I call in the main AJAX request?

+4
source share
4 answers

in jquery-1.4.2.js source add "yourFunction ()":

 function success() { yourFunction(); // If a local callback was specified, fire it and pass it the data if ( s.success ) { s.success.call( callbackContext, data, status, xhr ); } // Fire the global callback if ( s.global ) { trigger( "ajaxSuccess", [xhr, s] ); } } 

Edit:

in jquery-1.5.1.js source add "yourFunction ()":

 if (isSuccess) { yourFunction(); deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); } else { deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] ); } 
+3
source

Use the callback function defined in ajaxsetup.
Definition

 $.ajaxSetup({ beforeSend : function() { $('#ajaxLoader').show(); ... } }); 

and call later

 $.ajax({ beforeSend: function() { $.ajaxSettings.beforeSend(); // do other ... } }); 
+4
source

http://api.jquery.com/Ajax_Events/

You can use global events this way (listed below) instead of using ajaxSetup. Thus, the local will not override the global. Both will be completed.

Global events are fired in the document, invoking any handlers that can be listened to. You can listen to these events as follows:

 // the global event for success is "ajaxSuccess" $(document).bind("ajaxSuccess", function(){ // do your function }); 
+1
source

Preventing this will be due to an unfair change to jQuery or Ajaxify code, since $.ajaxSetup designed to overwrite old settings. Instead, can't you just use the onSuccess Ajaxify native property? (see http://max.jsrhost.com/ajaxify-jquery-plugin/ )

 $('.trigger').ajaxify({ target: '#element', onSuccess: function( options, data ) { example(); } }); 
0
source

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


All Articles