2015-05-03 56 views
0

問題是,當我嘗試使用此代碼從tmi.twitch.tv api使用url獲得日誌:http://tmi.twitch.tv/hosts?include_logins=1&target=70219146我得到了Systax錯誤意外令牌。該代碼是:JSON從tmi.twitch.tv api獲取數據的問題api

$(document).ready(function() { 
     $.getJSON("http://tmi.twitch.tv/hosts?include_logins=1&target=70219146&callback=?", function (data) { 
      console.log(data.hosts) 
}); 
    }) 

我可以使用PHP和JSON數組像這樣得到的數據:

$json_array = json_decode(file_get_contents('http://tmi.twitch.tv/hosts?include_logins=1&target=70219146'), true); 
echo $json_array['hosts']['0']['host_login']."</br>"; 

但是,這不是有沒有辦法使用HTML做到這一點?謝謝

回答

0

您試圖請求常規JSON作爲JSONp(&callback=?激活jQuery的JSONp請求方法),它實際上將響應嵌入到<script>中以執行它。但是,twitch API仍會返回JSON,這是無效的JavaScript。除非有辦法讓tmi.twitch.tv返回有效的JSONp,否則沒有辦法直接從JavaScript執行此操作,除非您使用像http://crossorigin.me/這樣的代理。

+0

所以唯一的方法是PHP?該死......反正謝謝! – user3607950