2011-09-03 49 views
0

我有一個問題,即一個.getJSON請求不拋出任何錯誤,或任何東西,它成功地進入回調函數(對我說的請求成功,是嗎?),但永遠不會退出。整個回調函數被執行,數據被正確讀取,但它永遠不會退出。.getJSON要求永遠不會退出

功能的getResult(選項){

 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」這樣的詞。

我是比較新的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

有一點要記住的是,你不應該期待讀取從上往下。 getJSON方法將執行,將「應該」記錄「out of JSON」,並且稍後在請求完成後,回調纔會運行。 – evan

+0

你能顯示* YQL * – naveen

+0

的YQL變量現在張貼以上。 – dlitwak

回答

1

你意識到JSON調用是異步的。這意味着當你調用getJSON時,你只是啓動ajax調用。該函數將立即返回(你應該看到「了JSON的」或者其他的語句(除非你有JS錯誤)。然後,當Ajax調用後成功,您應該看到「裏面的getJSON」之前就和「前如果你沒有看到這些,那麼你可能會在某處停止執行一些javascript錯誤,你應該檢查你的錯誤控制檯或調試器控制檯來尋找JS錯誤。猜測是要麼你不理解事件發生的順序,而是讓你感到困惑,或者你有JS錯誤正在停止執行。

如果你發佈你的getJSON的整個代碼,它也會幫助我們發現問題致電

+0

是的,我改變了評論,並意識到在ajax調用重複之前我看到了期望的響應。 – dlitwak