0

我想知道是否有任何适当的方法可以防止 ajax 请求在同时调用许多错误(同步或异步)时传递随机 404/500 错误

例如一个简单的 html 表格,其中包含一些行和列,以显示包含一些数据的 500-800 人的列表

走的路:

  1. 从后端通过选择(带有组织单位的html表单)获取ppl并将结果列表写入表格的第一列
  2. 通过他/她的 personId 获取每个人的事件(第一个请求的结果)
  3. 获取每个人的属性
  4. 为每个人获取角色
  5. 根据每个人的个人属性、角色和事件,从后端获取计算出来的东西。

伪代码:

function createTableData(){

  var selected;
  var personIds;

 //get personIds by selection and write them into table

 object.executeJSONRequest(...,
        {   _ts: new Date().getTime(),
            input: selected
        },function(data) {

                   // write result into table
                   // fill resourceIds with result param

        },..);

  getEvents(personIds);
  getAttribtues(personIds);
  getRoles(personIds);
  calculateStuff(personIds);

}


function getEvents(personIds){
   object.executeGetRequest('...',
        {   _ts: new Date().getTime(),
            ppl: personIds
        },function(data) {
                   //add result to table
        }
}

function getAttributes(personIds){...}  //and so on..

如果我连续调用所有这些方法,有时在加载属性时有时会出现随机错误,有时在加载角色时会出现随机错误,有时一切正常。

如果我称它们嵌套在每个 ajax 成功块中,例如

function createTableData(){

     object.executeJSONRequest(...
            {   _ts: new Date().getTime(),
                input: selected
            },function(data) {

                       getEvents(..);

                    });
 }

function getEvents(..){
  object.executeGETRequest(...
            {   _ts: new Date().getTime(),
                input: selected
            },function(data) {

                       getAttributes(..);

                    });
 }


function getAttributes(..){  ...,function(data){ getRole(..)});  and so on

一切正常。如果我尝试使用类似的解决方案

 document.ajaxStart/Stop(function(){  //if table columns i expected are filled do next}

我必须检查所有必要的表列是否已经填充了数据。

有没有我没想到的真正合适的解决方案?(jQuery 版本 1.2 /IE V7-8 ^^)

4

1 回答 1

0

“如果可能,更新 jquery 和 ie 版本”

首先,为什么要在一个进程中调用多个服务器调用。

最好将所有过程组合成一个请求和响应循环,在java代码中对数据进行所需的处理。不要在请求和响应中来回走动。

任何方式都可以使用返回 ajax 调用状态的 jquery ajax。

     $.post("url",function(result,status){{
            if(status == success){
    //continue....
    }else{
          alert("Sorry something went wrong");
         }
      }});

像这样检查它成功完成的所有请求天气。

我强烈建议您考虑将所有 ajax 调用合并到一个请求中。

于 2013-10-17T12:48:01.770 回答