简体   繁体   中英

multiple calls to ajax simultaneously

I'm trying to make multiple calls to Ajax, i have fields like time intervals and no of calls to ajax under that time period. Now the problem is, while making multiple calls to same Ajax, there may be chances of merging of data with the other data that were send to Ajax earlier. I am not sure that it will happen. Here my Ajax call.

callAjax = function () {
    var dataIn = inObj.data || {};
    var successFunc = inObj.success || function () {};
    var passOn = inObj.passOn || {};
    var myParams = {drape:1,type:'GET'};
    myParams.url = this.homeComingUrl;  
    $.extend(myParams,params);
    var data = this.fillAction(action,dataIn);
    if (myParams.drape) { vidteq.utils.drapeSheer(action); }
    var that = this;
    var magicCall = $.ajax({
       url:myParams.url,
       type:myParams.type,
       data:data,
       success: function (response) {
  // TBD we need better error handling
      if (myParams.drape) { vidteq.utils.undrapeCurtain(action); }
      successFunc(response,passOn);
      },
      error:function(response) { 
      if (myParams.drape) { vidteq.utils.undrapeCurtain(action); }
      that.gui.io.handleError(response); 
      }
    }); 
  }


saveEvents = function () {
   this.commitEditingEvent();
   var dataEvents = this.collectEventsToSave();
   //$('#calendar').fullCalendar('removeEvents');
   var that = this;
   if (vidteq.eTrainer==1) {
     dataEvents = arguments[0];
   }
   if (!dataEvents.length) { alert("Nothing to save");return; }
   this.callAjax('updateEvents',{
      data : { events : JSON.stringify(dataEvents) },
      success : function (response,passOn) {
      that.handleGetEvent(response,passOn);
      }
      },{type:'POST'}); 
 }

This may not be required for understanding the problem. If any body can explain how Ajax handles multiple calls, then it'll really helpful.

First line, your anonymous function isn't saved and isn't ran. Then. In each function, what does this refer to ? What is this context ? Is this window or do you call your function like saveEvents.apply( jQuery ) ?

JavaScript is powerful, when your want to run XMLHttpRequest (Ajax uses it), scripts are called when an event happen, like "server is found", "request is send", "file is reading", "file loaded"... for each state of your request. Ajax by jQuery help you to request asynchronous. You can request as many Ajax request as you would like in the same time. The important is to create a function happen in success case.

In this success function, you receive data, you compute it, then this function may call another Ajax request, and so on. When you chain requests like this to get the same file, we call it Ressource.

Ressource uses Ajax which uses XMLHttpRequest.

you need to do asynic :false in your ajax method

function isLoggedIn() {
    var isLoggedIn;
    $.ajax({
        async: false,
        // ...
        success: function(jsonData) {
            isLoggedIn = jsonData.LoggedIn
        }
    });
    return isLoggedIn 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM