这是我正在调用的API: https://api.github.com/search/repositories?q=language:python&sort=stars 它返回顶部加星标的 python 项目,并在通过浏览器触发时显示。 但是当我尝试从代码访问 json 键时,它显示未定义。 我做错了什么?
$.getJSON("https://api.github.com/search/repositories?q=language:python&sort=stars&callback=?", function(result){
alert(typeof(result));
alert(result.total_count);
alert(result.incomplete_results);
});
最佳答案
只需从 url 中删除回调:
$.getJSON("https://api.github.com/search/repositories?q=language:python&sort=stars", function(result){
alert(typeof(result));
alert(result.total_count);
alert(result.incomplete_results);
});
这是一个工作 fiddle : https://jsfiddle.net/a9npgduz/
关于javascript - 读取jsonp api调用返回的json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38479924/