Search code examples
jqueryajaxeventschaining

How would be the best way to chain ajax calls (one ajax call fire another one)?


I am trying to chain ajax calls: when i load html content in all element i want load it in others. I would want to do without hardcode the calls in each element with closures attached to the local ajax events. I tried to do with global ajax events like so:

$("#elem2").ajaxSuccess(function(e,x,opts) {
  var myUrl="server/elem2"
  if(conditionToAvoidRecursiveCalls)
    $(this).load(myUrl)
})

but i dont get how to define the conditionToAvoidRecursiveCalls: e.target!=this dont work and opts.url!==url dont avoid mutually recursive calls.

Is there another clever way?


Solution

  • Well the solution was simpler than i thougth, there is a option in ajax call which already do what i want (global:false):

    var html=$.ajax({url: url,global:false}).responseText
    $(this).html(html)
    

    global Boolean Default: true

    Whether to trigger global Ajax event handlers for this request. The default is true. Set to false to prevent the global handlers like ajaxStart or ajaxStop from being triggered. This can be used to control various Ajax Events.