简体   繁体   English

同时调用多个ajax

[英]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. 我正在尝试对Ajax进行多次调用,我有诸如时间间隔之类的字段,并且在该时间段内没有对Ajax的调用。 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. 现在的问题是,在对同一个Ajax进行多次调用时,可能会有数据与之前发送给Ajax的其他数据合并的可能性。 I am not sure that it will happen. 我不确定会不会发生。 Here my Ajax call. 这是我的Ajax电话。

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. 如果有任何机构可以解释Ajax如何处理多个调用,那么它将非常有帮助。

First line, your anonymous function isn't saved and isn't ran. 第一行,您的匿名函数未保存且未运行。 Then. 然后。 In each function, what does this refer to ? 在每个函数中, this指的是什么? What is this context ? 什么是this方面? Is this window or do you call your function like saveEvents.apply( jQuery ) ? 是此窗口还是您调用诸如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. JavaScript功能强大,当您要运行XMLHttpRequest(Ajax使用它)时,在事件发生时调用脚本,例如“找到服务器”,“请求发送”,“文件正在读取”,“文件已加载” ...针对您的请求的每种状态。 Ajax by jQuery help you to request asynchronous. jQuery Ajax帮助您请求异步。 You can request as many Ajax request as you would like in the same time. 您可以同时请求多个Ajax请求。 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. 在此成功函数中,您将接收数据,对其进行计算,然后该函数可以调用另一个Ajax请求,依此类推。 When you chain requests like this to get the same file, we call it Ressource. 当您像这样链接请求以获取相同文件时,我们将其称为Ressource。

Ressource uses Ajax which uses XMLHttpRequest. 资源使用Ajax,后者使用XMLHttpRequest。

you need to do asynic :false in your ajax method 您需要在ajax方法中执行asynic:false

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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