Javascript execution after ajax response

I want to execute the javascript part after the ajax response has been processed. The javascript function is generated dynamically during an ajax request and is in the ajax response. "complete" and "successful" events, so as not to do the job. I checked the ajax request in the Firebug console and the response was not processed when the full callback is executed.

Does not work: function reloadForm() { jQuery.ajax({ url: "<generate_form_url>", type: "GET", complete: custom_function_with_js_in_response() }); }; 

ajaxComplete does the job, but it runs for all ajax calls on the page. I want to avoid this. Is there a possible solution?

 $('#link_form').ajaxComplete(function() { custom_function_with_js_in_response(); }); 
+8
javascript jquery
source share
4 answers

you can also use $ .ajax (..). done (do_things_here ());

 $(document).ready(function() { $('#obj').click(function() { $.ajax({ url: "<url>" }).done(function() { do_something_here(); }); }); 

});

or is there another way

 $(document).ready(function() { $('#obj').click(function() { $.ajax({ url: "<url>", success: function(data){ do_something_with(data); } }) }); 

});

Please use this mechanism to share your problem and try solutions. It is very effective.

http://jsfiddle.net/qTDAv/7/ (PS: this is an example to try)

Hope to help

+11
source share

Checking (and deferring the call if necessary) and performing the callback function can work:

 // undefine the function before the AJAX call // replace myFunc with the name of the function to be executed on complete() myFunc = null; $.ajax({ ... complete: function() { runCompleteCallback(myFunc); }, ... }); function runCompleteCallback(_func) { if(typeof _func == 'function') { return _func(); } setTimeout(function() { runCompleteCallback(_func); }, 100); } 
+1
source share

It is impossible to help a lot without code. As a general example from jQuery ajax full page

 $('.log').ajaxComplete(function(e, xhr, settings) { if (settings.url == 'ajax/test.html') { $(this).text('Triggered ajaxComplete handler. The result is ' + xhr.responseHTML); } }); 

In ajaxComplete, you can decide to filter the URL for which you want to write code.

0
source share

Try specifying the function name without () in the ajax parameters:

 function reloadForm() { jQuery.ajax({ url: "<generate_form_url>", type: "GET", complete: custom_function_with_js_in_response }); }; 
0
source share

All Articles