[英]AngularJS Uncaught SyntaxError: Unexpected token :
Okay, iI tried several attempts and nothing is working 好吧,我尝试了几次尝试,没有任何工作
Checked the following answers 检查以下答案
questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu 问题/ 26262235 / JSONP返流,未捕获-的SyntaxError-意外性的令牌angularjs-routingnu
questions/16344933/angularjs-jsonp-not-working/16352746#16352746 问题/ 16344933 / angularjs-JSONP - 不工作/ 16352746#16352746
questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery 问题/ 19269153 / JSONP请求功能于angularjs-犯规工作样的-的jQuery
questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp 问题/ 19669044 / angularjs-越来越语法错误 - 在 - 返回 - JSON-从-HTTP-JSONP
And non of them solved my problem. 而且他们没有解决我的问题。
I would like to use Giant Bombs API: http://www.giantbomb.com/api/ 我想使用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)
});
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()包装数据的结果相同
The reason of this is, that angular requires service returns jsonp, but it doesn't 原因是,角度要求服务返回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. 之后我可以看到关于api键的响应错误 - 这可能在你的实例中没问题。 So I uppose you will get correct JSONP
所以我反对你会得到正确的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)
});
The last one recommendation, do not use $scope in controllers, rather use "controller as" syntax. 最后一条建议,不要在控制器中使用$ scope,而是使用“controller as”语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.