【问题标题】:Random error with the same number of calls in ajaxajax中调用次数相同的随机错误
【发布时间】:2019-03-10 14:18:36
【问题描述】:

从 ajax “成功”调用包含 ajax 的函数时的奇怪行为。

jQuery.ajax(
              { 
                url: urlWMS,
                dataType: 'jsonp',
                jsonpCallback: 'parseResponse',
                error: function(xhr, status, error) {
                    console.log("err: " + error);
                },
                success: function(jsonp) {                    
                    if(jsonp.features.length > 0) {
                        objResultats = [];
                        for(i = 0; i < jsonp.features.length; i++) {
                          objResultats.push({
                              carrer: jsonp.features[i].properties.des,
                              numero: sNumero,
                              idnom: jsonp.features[i].id.split('.')[1],
                              punt: jsonp.features[i].geometry.coordinates[0][0].toString()
                          });
                        }
                    }
                    if(sNumero.length > 0) {
                      //buscar punts exactes
                      for(i = 0; i < objResultats.length; i++) {
                        getPoint(i, objResultats[i].idnom, objResultats[i].numero); <----- this is the function that makes another call that randomly returns error
                      }
                    }
                }

函数“getPoint”包含一个ajax调用:

Query.ajax(
            { 
              url: urlWMS,
              //type: 'GET',
              dataType: 'jsonp',
              jsonpCallback: 'parseResponse',
              error: function(xhr, status, error) {
                  console.log("err: " + error);
              },
              success: function(jsonp) {
                  //strRespostaJSON = JSON.stringify(jsonp);
                  if(jsonp.features.length > 0) {
                    sPuntExacte = jsonp.features[0].geometry.coordinates.toString();
                  }
              }
            }

所以,我遇到的问题是函数“getPoint”返回例如 5 个点,但有时它随机返回 4 ok 和 1 错误,或 3 ok 2 错误。 控制台显示:

This time fail 3 times(of 5 calls)

This time fail 2 times(of 5 calls)

如果错误是随机的,那很奇怪......我认为这是在调用之间放置一个睡眠,因为我在某处读到调用重复相同的 jsonpCallback 会产生一些问题,是真的吗?

【问题讨论】:

    标签: jquery ajax callback jsonp


    【解决方案1】:

    好的,对于那些有同样问题的人,我找到了解决方案。这是通过在调用之间设置一个 settimeout 间隔,因此它们都返回 ok。

    第一个ajax成功里面,新的代码是:

    if(sNumero.length > 0) {
                      //buscar punts exactes
                      for(i = 0; i < objResultats.length; i++) {
                        var tick = function(i) {
                            return function() {
                              getPuntCarrerNumero(i, objResultats[i].idnom, objResultats[i].numero);
                              console.log("punt i=" + i + " - idnom=" + objResultats[i].idnom + " actualitzat");
                            }
                        };
                        setTimeout(tick(i), 500 * i);
                      }
                    }
    

    而这种行为正是我所希望的:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多