【问题标题】:Syntax Error: missing ; before statement [JSONP, Instagram API]语法错误:缺少;声明前 [JSONP, Instagram API]
【发布时间】:2015-07-28 11:46:05
【问题描述】:

我正在尝试从 Instragram API 获取 json 响应,但没有结果。

我使用下面的代码获取数据,但出现错误Uncaught SyntaxError: Unexpected token :

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            type : "Get",
            url :"https://api.instagram.com/v1/media/search?access_token=MYTOKEN&lat=00.0&lng=00.0&distance=30000?callback=?",
            dataType :"jsonp",
            jsonp: false,
            jsonpCallback: " ",
            success : function(instagramres){
                alert(instagramres);
                document.write(instagramres.meta.code);
            },
            error : function(httpReq,status,exception){
                alert(status+" "+exception);
            }
        });
    });
</script>

我已将jsonp:false 更改为jsonp:true,但没有任何结果。

我通过 chrome 开发控制台截屏:

【问题讨论】:

  • 什么时候会抛出错误?
  • 删除 jsonp: falsejsonpCallback: " " 并将 Get 更改为 get。最好使用 JSON 的 ajax 简写 -> http://api.jquery.com/jquery.getjson/
  • jsonpCallback: " testcallback", 我收到了parsererror: Error testcallback was not called。如果我删除这条线,我会得到parsererror Error: jQuery181011872481792222633_1438084410647 was not called
  • @lshettyl 如果我删除jsonp: falsejsonpCallback 并更改get,我会得到parsererror Error: jQuery18107422583257549523_1438084470015 was not called

标签: javascript jquery json jsonp instagram-api


【解决方案1】:

正如我在评论中所说,您不需要type(默认为get)和jsonpCallback(除非您想拥有自己的回调方法)等参数。您可以通过jsonp 参数指定回调。看看下面的代码,看看它是否适合你。

$(document).ready(function(){
    $.ajax({
        url: "https://api.instagram.com/v1/media/search?access_token=MYTOKEN&lat=00.0&lng=00.0&distance=30000",
        // The name of the callback parameter
        jsonp: "callback",
        // Tell jQuery we're expecting JSONP
        dataType: "jsonp",
        // Deal with the response
        success: function(instagramres){
            alert(instagramres);
        }
    });
});

【讨论】:

  • 你是对的!! Type: "get"jsonp: "false" 造成了问题。现在我修复它。谢谢@lshettyl
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 2017-03-25
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多