.getJSON请求永不退出

时间:2011-09-03 04:30:29

标签: jquery ajax error-handling exit getjson

我遇到一个问题,即.getJSON请求不会抛出任何错误,或者其他什么,它成功进入回调函数(它告诉我请求成功,是吗?)但是永远不会退出。执行整个回调函数,正确读取数据,但它永远不会退出。

function 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);

打印出“之前”的console.log命令。没有错误被抛出。但是,它永远不会到达“out of JSON”的console.log。

当我尝试使用控制台进行调试时,我发现它被卡在jquery.min.js代码的一部分中,虽然最小化,我可以告诉其中有“abort”和“timeout”等字样。 / p>

我对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

1 个答案:

答案 0 :(得分:1)

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

我的猜测是,你要么不理解事情发生的顺序,要么你感到困惑,或者你有JS错误正在停止执行。

如果您发布getJSON调用的完整代码,它还可以帮助我们识别问题。

相关问题