2009-07-02 170 views
2

$(document).ready(function() {      
    $('form#search').bind("submit", function(e){        
      e.preventDefault(); 
      $('#content').html(''); 

// Define the callback function 
    function getGeo(jsonData) {  
    $('#content').append('

'+jsonData.rank+'跨域JSON響應失敗

'); bObj.removeScriptTag(); } // The web service call var req = 'http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=getGeo'; // Create a new request object bObj = new JSONscriptRequest(req); // Build the dynamic script tag bObj.buildScriptTag(); // Add the script tag to the page bObj.addScriptTag(); }); });

林試着使用跨域JSON請求,以獲得JSON數據是


{ 
"user_id":"3190399", 
"user_name":"Anand_Dasgupta", 
"followers_current":"86", 
"date_updated":"2009-06-04", 
"url":"", 
"avatar":"205659924\/DSC09920_normal.JPG", 
"follow_days":"0","started_followers":"86", 
"growth_since":0, 
"average_growth":"0", 
"tomorrow":"86", 
"next_month":"86", 
"followers_yesterday":"86", 
"rank":176184, 
"followers_2w_ago":null, 
"growth_since_2w":86, 
"average_growth_2w":"6", 
"tomorrow_2w":"92", 
"next_month_2w":"266", 
"followersperdate":[] 
} 

IM從URL

http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=getGeo

獲取JSON數據

但是這段代碼似乎不起作用。 如果有人能夠以某種方式拋光代碼或提供任何迴應,將不勝感激。 謝謝

回答

2

Stobor是在正確的軌道上。我訪問了該頁面,其中包含您明顯使用的課程和操作方法信息:http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html。那裏的腳本利用了雅虎用來指定包裝JSON數據的回調函數的回調=值(從而使其成爲JSON P數據)。您的URL中有一個回調= getGeo,但TwitterCounter API確實不是有一種指定回調函數的方法。我創建使用您在使用這些代碼的完整HTML頁面:

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Twittercounter API Test</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <script type="text/javascript" src="jsr_class.js"></script> 
    <script type="text/javascript"> 
    var bObj; 

    // Define the callback function 
    function getGeo(jsonData) {  
    $('#content').append(''+jsonData.rank+''); 
    bObj.removeScriptTag(); 
    } 

    $(document).ready(function() {      
     $('form#search').bind("submit", function(e){        
       e.preventDefault(); 
       $('#content').html(''); 
       // The web service call 
       var req = 'http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=getGeo'; 

       // Create a new request object 
       bObj = new JSONscriptRequest(req); 

       // Build the dynamic script tag 
       bObj.buildScriptTag(); 

       // Add the script tag to the page 
       bObj.addScriptTag(); 
     }); 
    }); 
    </script> 
    </head> 

    <body> 
    <form id="search"> 
    <input type="submit" id="search" value="Get Info" /> 
    </form> 
    <div id="content"> 
    </div> 
    </body> 
    </html> 

,當我啓動按鈕螢火蟲給了我一個錯誤。原因是基於這篇文章的原始文章:

這是一個有效的JavaScript語句,因此它可以是返回JavaScript的腳本標記的目標(原始JSON數據,沒有回調函數,不是有效的JavaScript語句,因此如果它是腳本標記的目標,它將無法加載)。爲了比較,請在這裏查看此調用的XML版本。

「有效的JavaScript語句」是一個包含實際數據的函數名。

如果Twittercounter允許JSONP請求並讓你指定包裝函數,Stobor的解決方案將是完美的。事實上,您必須創建自己的代理來充當中介。我在how to create one using PHP on my blog上有一個例子。

2

這裏只是猜測,但是當jsonp回調被觸發時,getGeo函數是否超出範圍?也許嘗試從$(document).ready()塊中移出getGeo函數?

編輯:或者,你已經使用jQuery,對吧? jQuery將爲你做跨域的東西!

$(document).ready(function() 
{ 
    $.getJSON('http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=?',function(jsonData){ 
     $('#content').append(' 
'+jsonData.rank+' 
'); 
    }); 
}); 
+0

我同意。就是說,在我看來,這個錯誤。 – Boldewyn 2009-07-02 12:47:40

+0

我以前試過使用getJSON,並且失敗了。它似乎沒有顯示任何東西。 Il嘗試移動getGeo功能並檢查。 – anand 2009-07-02 13:02:37