【问题标题】:can't read a json come from insta无法读取来自 insta 的 json
【发布时间】:2017-05-14 12:04:44
【问题描述】:

我做了一个这样的项目,但是在解析一个来自 instagram api 的 json 文件时,我无法读取它 我的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>tst</title>
<script src="../jq.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".send").click(function(){
        var name =  $("#name").val();
        var my_url= "https://api.instagram.com/v1/tags/"+name+"?access_token=2307573123.2c01fc8.d2a19d4145c84d59962c0db8d418d2a8";
        $("#name").val("");
        $.ajax({
            type: 'GET',
            url: my_url,
            dataType: 'jsonp',
            function(response){
                //how????
            }
            });
        });
    });
</script>
</head>
<body>
  <p id="p1"></p>
  <input type="search" id="name" />
  <input type="button" class ="send"  value="send" />
</body>
</html>

我只想读一个共振。我可以解析它,但我看不懂。 //我放的地方 如何??? 是我阅读的地方

【问题讨论】:

    标签: javascript jquery json instagram instagram-api


    【解决方案1】:

    ajax 调用出错,没有得到正确响应,应该是这样的:

    $(document).ready(function(){
        $(".send").click(function(){
        var name =  $("#name").val();
        var my_url= "https://api.instagram.com/v1/tags/"+name+"?access_token=2307573123.2c01fc8.d2a19d4145c84d59962c0db8d418d2a8";
        $("#name").val("");
        $.ajax({
            type: 'GET',
            url: my_url,
            dataType: 'jsonp'
        }).done(function(response){
                //response is our object with data
            }
            );
        });
    });
    

    【讨论】:

    • tnx 很多。我的问题解决了。我会接受你的答案,因为 sson as possibel :)
    【解决方案2】:

    API 响应包含一个数据属性:

    获取:https://api.instagram.com/v1/tags/stackoverflow?access_token=xxx

    {"data": {"name": "stackoverflow", "media_count": 10769}, "meta": {"code": 200}}
    

    访问值:

    success:function(response){
      //how????
      alert(response.data.name + ' have ' + response.data.media_count + ' media');
    }
    

    【讨论】:

    • 应该是success:function(response) { ... }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2023-03-29
    • 2020-12-08
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多