jquery - .getJSON请求永不退出

标签 jquery ajax error-handling exit getjson

我遇到了一个问题,即.getJSON请求不会引发任何错误或任何错误,它成功进入了回调函数(对我来说,请求成功了,是吗?),但从未退出。整个回调函数均已执行,数据已正确读取,但从不退出。

函数getResult(option){

     console.log("inside getResult");
        $.getJSON(yql, function(data){
          console.log("inside getJSON");
          var directions = '';
          $(data.query.results.div).each(function(i,element){
            htmlimg=(typeof(element.img) != 'undefined') ? '<img src="'+element.img.src+'" alt="'+element.img.alt+'">' : '';
            switch(typeof(element.p)){
              case 'undefined':
                htmlp = '';
                break;
              case 'string':
                if(element.p == 'Alternative routes:'){
                  //end looping
                  return false;
                }
                htmlp = '<p>' + element.p + '</p>';
                break;
              case 'object':
                function showhtmlp(element){
                  var s = '';
                  if(typeof(element.content != 'undefined')){
                    s += element.content;
                  }
                  if(typeof(element.a) != 'undefined'){
                    s += element.a.content;
                  }
                  if(typeof(element.br) != 'undefined'){
                    s+='<br>';
                  }
                  return s
                }

                htmlp = '<p>';
                if(typeof(element.p[0]) != 'undefined'){
                  $(element.p).each(function(i, data){
                    htmlp += showhtmlp(data);
                  });
                } else {
                  htmlp += showhtmlp(element.p);
                }
                htmlp += '</p>';
                break;
            }
            directions += '<div>'+htmlimg+htmlp+'</div>';
    console.log("Directions: ");
    console.log(directions);
          });
    console.log("OUT OF DIRECTIONS EACH FUNCTION/LOOP");
          directions += '<div><a href="' + data.query.diagnostics.url.content + '" target="_blank" title="See on Google Maps"><img src="images/link.png" alt="Link" class="smallicon">View trip on Google Transit</a></div>';
    console.log("step1");
          trip.transit.routes[option].directions = directions;
    console.log("step2");
          $('#transitoption'+option).html(directions);
    console.log("step3");
        });//end of .getJSON
    console.log("out of JSON");
      }//end of getResult?
    console.log("out of Result");
      getResult(option);

打印出“before end”的console.log命令。没有引发任何错误。但是,它永远不会到达“out of JSON”的console.log。

当我尝试使用控制台进行调试时,我看到它卡在了jquery.min.js代码的一部分中,尽管将其最小化,但我可以说其中包含“中止”和“超时”之类的词。

我对ajax请求比较陌生,所以请非常具体。

yql变量是:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fmaps.google.com%2Fm%2Fdirections%3Fsaddr%3D37.78414%2C%2B-122.44108%26daddr%3D37.61688%2C%2B-122.38400999999999%26dirflg%3Dr%26ri%3D0%26date%3D2011-09-02%26time%3D9%3A21pm%22%20and%20xpath%3D'%2F%2Fdiv%2Fdiv'&format=json&diagnostics=true

最佳答案

您是否意识到JSON调用是异步的。这意味着,当您调用getJSON时,您只是在启动ajax调用。该函数将立即返回(并且您应该立即在其他任何一条语句之前看到“out of JSON”(除非您遇到JS错误)。然后,当ajax调用稍后成功时,您应该看到“inside getJSON”和“before”成功处理程序功能中的“结束”。如果您没有看到这些,则可能是某些javascript错误正在停止执行。您应该检查错误控制台或调试器控制台以查找JS错误。

我的猜测是,要么您不了解事情发生的顺序,使您感到困惑,要么是您的JS错误导致执行停止。

如果您将整个代码发布给getJSON调用,这也将帮助我们确定问题。

关于jquery - .getJSON请求永不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7291180/

相关文章:

angular - 如何测试Promise catch block 是否设置了this.error

ruby-on-rails - 无效的创建方法不会引发任何错误

javascript - Skrollr 在 chrome 中相对自上而下很早就火了

jquery - TableSorter - 组插件

javascript - setTimeinterval 内的 Jquery setTimeout 不起作用

javascript - SlideToggle 删除其他事件类

javascript - 如何从安全 (HTTPS) 页面访问本地不安全 (HTTP) 服务器

javascript - XMLHttpRequest() 为具有绝对或相对路径的本地主机发送 NS_ERROR_FAILURE

php - 使用 PHP 将数组传递给 Ajax

matlab - 在Matlab中防止 “MATLAB:unassignedOutputs”