JSON从tmi.twitch.tv api获取数据的问题 [英] Problems with JSON getting data from tmi.twitch.tv api
问题描述
问题是当我尝试使用以下代码通过url从tmi.twitch.tv api获取日志时:
Problem is when i try to use this code to get the log from tmi.twitch.tv api using url: http://tmi.twitch.tv/hosts?include_logins=1&target=70219146 i get Systax Error Unexpected Token. The code is:
$(document).ready(function() {
$.getJSON("http://tmi.twitch.tv/hosts?include_logins=1&target=70219146&callback=?", function (data) {
console.log(data.hosts)
});
})
我可以使用php和json数组获取数据,如下所示:
I can get the data using php and json array like this:
$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中使用它吗?谢谢
But isnt there a way to use do this in html? thanks
推荐答案
您正尝试将常规JSON请求为JSONp(&callback=?
激活jQuery的JSONp请求方法),该方法实际上将响应嵌入到<script>
中以执行它.但是,twitch API仍返回无效的JavaScript JSON.除非有一种方法可以使tmi.twitch.tv返回有效的JSONp,否则除非您使用
You are trying to request regular JSON as JSONp (&callback=?
activates jQuery's JSONp request method) which actually embeds the response into a <script>
to execute it. However, the twitch API still returns JSON which is not valid JavaScript. Unless there is a way to make tmi.twitch.tv return valid JSONp, there is no way to do this directly from JavaScript unless you use a proxy like http://crossorigin.me/.
这篇关于JSON从tmi.twitch.tv api获取数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!