AngularJS未捕获的SyntaxError:意外的令牌:

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

检查了以下答案

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

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

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

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

  • 他们中没有人解决了我的问题。

    我想使用巨型炸弹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”语法。

    链接地址: http://www.djcxy.com/p/46019.html

    上一篇: AngularJS Uncaught SyntaxError: Unexpected token :

    下一篇: Uncaught SyntaxError: Unexpected token when using jQuery Ajax in Codeignitor