.getJSON 请求永远不会退出

发布于 2024-12-02 21:44:58 字数 3155 浏览 1 评论 0原文

我遇到一个问题,.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);

打印出“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

I'm having a problem where a .getJSON request doesn't throw any errors, or anything, it successfully enters the callback function (which says to me the request succeeded, yes?) but never exits. The entire callback function is executed, the data is read correctly, but it never exits.

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

the console.log command of "before end" is printed out. No errors are thrown. However, it never reaches the console.log of "out of JSON".

When I tried to use the console to debug I saw it getting stuck in a portion of the jquery.min.js code that, while minimized, I could tell had words like "abort" and "timeout" in it.

I'm relatively new to ajax requests, so please be very specific.

the yql variable is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-12-09 21:44:58

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

我的猜测是你不明白事情发生的顺序会让你感到困惑,或者你有停止执行的 JS 错误,

如果你发布 getJSON 调用的完整代码,它也将帮助我们识别问题。

Do you realize that the JSON call is asynchronous. That means that when you call getJSON, you are just initiating the ajax call. That function will return immediately (and you should see "out of JSON" right away before either of the other statements (unless you have JS errors). Then, when the ajax call succeeds later, you should see "inside getJSON" and "before end" from the success handler function. If you aren't seeing these, then you may have some javascript errors somewhere that are halting execution. You should be checking your error console or debugger console to look for JS errors.

My guess is that either you aren't understanding the order that things happen in and that has you confused or you have JS errors that are halting execution.

It would also help us identify problems if you post the whole code for your getJSON call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文