2013-01-15 92 views
1

嗨每一個我試圖從本網站的Web服務中獲取此json文件:「http://172.20.192.218:8080/DC_Local/rest/datacorrelation?type=/soccer/football_team & limit = 20 & callback =?「未捕獲的SyntaxError:意外的令牌JSON

但是我得到這個「未捕獲的SyntaxError:意外的令牌」 但我不知道什麼是錯的。

這是我調用JSON文件代碼:

var pts = []; 
    $.getJSON("http://172.20.192.218:8080/DC_Local/rest/datacorrelation?type=/soccer/football_team&limit=20&callback=?", function(data) { 
     //JSON.parse(data); 

     $.each(data, function(i,v){ 
      pts.push([v.topic, v.audience.length]); 
     }); 
    }); 

,這是我的JSON文件,我試圖讓:

{"data":[{"id":"/en/arsenal_fc","topic":"Arsenal F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ajax_amsterdam","topic":"Ajax Amsterdam","audience":[],"type":"/soccer/football_team"},{"id":"/en/az_alkmaar","topic":"AZ Alkmaar","audience":[],"type":"/soccer/football_team"},{"id":"/en/as_roma","topic":"A.S. Roma","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_milan","topic":"A.C. Milan","audience":[],"type":"/soccer/football_team"},{"id":"/en/acf_fiorentina","topic":"ACF Fiorentina","audience":[],"type":"/soccer/football_team"},{"id":"/en/bristol_city_fc","topic":"Bristol City F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/charlton_athletic_fc","topic":"Charlton Athletic F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_chievoverona","topic":"A.C. Chievo Verona","audience":[],"type":"/soccer/football_team"},{"id":"/en/chelsea_fc","topic":"Chelsea F.C.","audience":[{"information":[{"category":"Athlete","source":"Frank Lampard"}],"username":"Tan Jercia","userid":"100003921730958"},{"information":[{"category":"Athlete","source":"Didier Drogba"},{"category":"Athlete","source":"Frank Lampard"},{"category":"Professional sports team","source":"Chelsea Football Club"},{"category":"favorite_athletes","source":"Peter Cech"},{"category":"favorite_athletes","source":"Didier Drogba"},{"category":"favorite_athletes","source":"Frank Lampard"}],"username":"Ang Peterson","userid":"100003914111287"}],"type":"/soccer/football_team"},{"id":"/en/england_national_football_team","topic":"England national football team","audience":[],"type":"/soccer/football_team"},{"id":"/en/fulham_fc","topic":"Fulham F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/f_c_den_bosch","topic":"F. C. Den Bosch","audience":[],"type":"/soccer/football_team"},{"id":"/en/hellas_verona_fc","topic":"Hellas Verona F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/fc_internazionale_milano","topic":"F.C. Internazionale Milano","audience":[],"type":"/soccer/football_team"},{"id":"/en/juventus_fc","topic":"Juventus F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/liverpool_fc","topic":"Liverpool F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/luton_town_fc","topic":"Luton Town F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/manchester_united_fc","topic":"Manchester United F.C.","audience":[{"information":[{"category":"Professional sports team","source":"Manchester United"}],"username":"Tan Jercia","userid":"100003921730958"}],"type":"/soccer/football_team"},{"id":"/en/plymouth_argyle_fc","topic":"Plymouth Argyle F.C.","audience":[],"type":"/soccer/football_team"}]} 
+0

你不需要'JSON.parse()來調用'。不確定,不管你是否使用過,當你有錯誤時。 – Sirko

+0

它是HTTP錯誤(發佈代碼)還是JS錯誤? –

+0

爲我返回404。可能是HTTP錯誤。 –

回答

1

你必須寫$.each(data.data, function(i,v){而不是$.each(data, function(i,v){

樣品:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Add many Elements</title> 
     <script src="jquery.js"></script> 
     <script> 
      $(document).ready(function() { 
      var dataString = '{"data":[{"id":"/en/arsenal_fc","topic":"Arsenal F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ajax_amsterdam","topic":"Ajax Amsterdam","audience":[],"type":"/soccer/football_team"},{"id":"/en/az_alkmaar","topic":"AZ Alkmaar","audience":[],"type":"/soccer/football_team"},{"id":"/en/as_roma","topic":"A.S. Roma","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_milan","topic":"A.C. Milan","audience":[],"type":"/soccer/football_team"},{"id":"/en/acf_fiorentina","topic":"ACF Fiorentina","audience":[],"type":"/soccer/football_team"},{"id":"/en/bristol_city_fc","topic":"Bristol City F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/charlton_athletic_fc","topic":"Charlton Athletic F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_chievoverona","topic":"A.C. Chievo Verona","audience":[],"type":"/soccer/football_team"},{"id":"/en/chelsea_fc","topic":"Chelsea F.C.","audience":[{"information":[{"category":"Athlete","source":"Frank Lampard"}],"username":"Tan Jercia","userid":"100003921730958"},{"information":[{"category":"Athlete","source":"Didier Drogba"},{"category":"Athlete","source":"Frank Lampard"},{"category":"Professional sports team","source":"Chelsea Football Club"},{"category":"favorite_athletes","source":"Peter Cech"},{"category":"favorite_athletes","source":"Didier Drogba"},{"category":"favorite_athletes","source":"Frank Lampard"}],"username":"Ang Peterson","userid":"100003914111287"}],"type":"/soccer/football_team"},{"id":"/en/england_national_football_team","topic":"England national football team","audience":[],"type":"/soccer/football_team"},{"id":"/en/fulham_fc","topic":"Fulham F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/f_c_den_bosch","topic":"F. C. Den Bosch","audience":[],"type":"/soccer/football_team"},{"id":"/en/hellas_verona_fc","topic":"Hellas Verona F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/fc_internazionale_milano","topic":"F.C. Internazionale Milano","audience":[],"type":"/soccer/football_team"},{"id":"/en/juventus_fc","topic":"Juventus F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/liverpool_fc","topic":"Liverpool F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/luton_town_fc","topic":"Luton Town F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/manchester_united_fc","topic":"Manchester United F.C.","audience":[{"information":[{"category":"Professional sports team","source":"Manchester United"}],"username":"Tan Jercia","userid":"100003921730958"}],"type":"/soccer/football_team"},{"id":"/en/plymouth_argyle_fc","topic":"Plymouth Argyle F.C.","audience":[],"type":"/soccer/football_team"}]}' 
       var pts = []; 
     var data = JSON.parse(dataString); 

     $.each(data.data, function(index,data){ 
      pts.push([data.topic, data.audience.length]); 
     }); 
      }); 
     </script> 
    </head> 
    <body> 
     <div class="testDiv"> 
      <div class="testDivPos"></div> 
     </div> 
    </body> 
</html> 
+0

你的第一句話對我來說真的沒有意義。 Bot變種似乎是相同的。 – Sirko

+0

對不起,我打算第二個例子應該說*'$。each(data,function' – Stefan

相關問題