AngularJS Uncaught SyntaxError:意外令牌: [英] AngularJS Uncaught SyntaxError: Unexpected token :

查看:179
本文介绍了AngularJS Uncaught SyntaxError:意外令牌:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Okay, iI tried several attempts and nothing is working

检查了以下答案

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

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

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

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

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

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

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

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

没有一个人解决了我的问题.

And non of them solved my problem.

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

I would like to use Giant Bombs 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)
    });

错误

Uncaught SyntaxError: Unexpected token :

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

Could someone give me a hint?

由于它确实令人沮丧,我什至用JSON_CALLBACK()相同的结果包装了数据

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

推荐答案

原因是,角度服务需要返回jsonp,但不是这样

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

您的代码会发出以下请求:

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

因此应将用于回调的参数命名为:json_callback,而不仅仅是回调.

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

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

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

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

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(function (response) {
        $scope.data = response.data;
        console.log(response.data)
    });

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

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

这篇关于AngularJS Uncaught SyntaxError:意外令牌:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆