繁体   English   中英

AngularJS Uncaught SyntaxError:意外的令牌:

[英]AngularJS Uncaught SyntaxError: Unexpected token :

好吧,我尝试了几次尝试,没有任何工作

检查以下答案

  1. 问题/ 26262235 / JSONP返流,未捕获-的SyntaxError-意外性的令牌angularjs-routingnu

  2. 问题/ 16344933 / angularjs-JSONP - 不工作/ 16352746#16352746

  3. 问题/ 19269153 / JSONP请求功能于angularjs-犯规工作样的-的jQuery

  4. 问题/ 19669044 / angularjs-越来越语法错误 - 在 - 返回 - JSON-从-HTTP-JSONP

而且他们没有解决我的问题。

我想使用Giant Bombs API: http//www.giantbomb.com/api/

是的,我看了一下论坛帖子没什么作用。

$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)
    });

错误

Uncaught SyntaxError: Unexpected token :

有人能给我一个暗示吗?

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

原因是,角度要求服务返回jsonp,但事实并非如此

您的代码执行此请求:

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

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

{"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":[]}

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

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

之后我可以看到关于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":[]}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM