【问题标题】:Read the json object returned by jsonp api call读取jsonp api调用返回的json对象
【发布时间】:2016-11-23 14:40:20
【问题描述】:

这是我调用的 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);

});

【问题讨论】:

    标签: javascript jquery json api jsonp


    【解决方案1】:

    只需从 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);
    
    });
    

    这是一个有效的小提琴: https://jsfiddle.net/a9npgduz/

    【讨论】:

    猜你喜欢
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    相关资源
    最近更新 更多