AngularJS Uncaught SyntaxError: Unexpected token :
AngularJS Uncaught SyntaxError: Unexpected token :
好的,我尝试了几次都没有用
检查了以下答案
questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu
questions/16344933/angularjs-jsonp-not-working/16352746#16352746
questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery
questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-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() 包装数据相同的结果
原因是,angular 需要服务 returns 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 函数不是直接获取数据,而是 response,它是携带数据的对象,所以:
.then(function (response) {
$scope.data = response.data;
console.log(response.data)
});
最后一个建议,不要在控制器中使用 $scope,而是使用 "controller as" 语法。
好的,我尝试了几次都没有用
检查了以下答案
questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu
questions/16344933/angularjs-jsonp-not-working/16352746#16352746
questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery
questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-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() 包装数据相同的结果
原因是,angular 需要服务 returns 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 函数不是直接获取数据,而是 response,它是携带数据的对象,所以:
.then(function (response) {
$scope.data = response.data;
console.log(response.data)
});
最后一个建议,不要在控制器中使用 $scope,而是使用 "controller as" 语法。