跨域JSON响应失败

问题描述:


$(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数据

但是这段代码似乎不起作用。 如果有人能够以某种方式抛光代码或提供任何回应,将不胜感激。 谢谢

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上有一个例子。

这里只是猜测,但是当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