javascript AngularJS 未捕获语法错误:意外标记:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33451322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:30:12  来源:igfitidea点击:

AngularJS Uncaught SyntaxError: Unexpected token :

javascriptangularjsrequestjsonp

提问by

Okay, iI tried several attempts and nothing is working

好的,我尝试了几次,但没有任何效果

Checked the following answers

检查了以下答案

  1. questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu

  2. questions/16344933/angularjs-jsonp-not-working/16352746#16352746

  3. questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery

  4. questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp

  1. 问题/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu

  2. 问题/16344933/angularjs-jsonp-not-working/16352746#16352746

  3. 问题/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery

  4. 问题/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp

And non of them solved my problem.

他们都没有解决我的问题。

I would like to use Giant Bombs API: http://www.giantbomb.com/api/

我想使用巨型炸弹 API:http: //www.giantbomb.com/api/

Yes i took a look at the forum posts nothing works.

是的,我查看了论坛帖子,没有任何效果。

$http({
        method: 'JSONP',
        url: 'http://www.giantbomb.com/api/game/3030-4725/',
        params: {
            api_key: $rootScope.api_key,
            format: 'jsonp',
            callback: 'JSON_CALLBACK'
        }
    }).then(function (data) {
        $scope.data = data;
        console.log($scope.data)
    });

Error

错误

Uncaught SyntaxError: Unexpected token :

Could someone give me a hint?

有人可以给我一个提示吗?

Because its really frustrating, I even wrapped the data with JSON_CALLBACK() same result

因为它真的很令人沮丧,我什至用 JSON_CALLBACK() 相同的结果包装了数据

采纳答案by Ví?a Pl?ek - angular.cz

The reason of this is, that angular requires service returns jsonp, but it doesn't

这样做的原因是,angular 需要服务返回 jsonp,但它没有

Your code does this request:

您的代码执行此请求:

http://www.giantbomb.com/api/game/3030-4725/?json_callback=angular.callbacks._0&format=jsonp

And in network console you can see the response:

在网络控制台中,您可以看到响应:

{"error":"'jsonp' format requires a 'json_callback' arguement","limit":0,"offset":0,"number_of_page_results":0,"number_of_total_results":0,"status_code":103,"results":[]}

so the parameter for callback should be named: json_callback, not just callback.

所以回调的参数应该命名为:json_callback,而不仅仅是回调。

$http({
        method: 'JSONP',
        url: 'http://www.giantbomb.com/api/game/3030-4725/',
        params: {
            format: 'jsonp',
            json_callback: 'JSON_CALLBACK'
        }
    })

After that I can see in response error about api key - this will probably be ok in your instance. So I uppose you will get correct JSONP

之后,我可以看到有关 api 密钥的响应错误 - 这在您的实例中可能没问题。所以我想你会得到正确的 JSONP

http://www.giantbomb.com/api/game/3030-4725/?json_callback=angular.callbacks._0&format=jsonp

{"error":"Invalid API Key","limit":0,"offset":0,"number_of_page_results":0,"number_of_total_results":0,"status_code":100,"results":[]}

The other problem would be that your function in then doesn't get data directly, but response, which is objects which carry data, so:

另一个问题是你的函数 then 不直接获取数据,而是响应,它是携带数据的对象,所以:

.then(function (response) {
        $scope.data = response.data;
        console.log(response.data)
    });

The last one recommendation, do not use $scope in controllers, rather use "controller as" syntax.

最后一个建议,不要在控制器中使用 $scope,而是使用“controller as”语法。