【发布时间】:2016-10-09 12:56:38
【问题描述】:
我正在尝试在我的 Angular 应用程序中向 http://catfacts-api.appspot.com/api/facts' 发出请求。我使用$http 服务对页面进行了http GET,但出现跨源错误:
$http({
method: 'GET',
url: 'http://catfacts-api.appspot.com/api/facts'
}).then(function(response) {
console.log(response);
});
但我得到一个跨源错误
XMLHttpRequest cannot load http://catfacts-api.appspot.com/api/facts. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://cat-facts-alexwohlbruck.c9users.io' is therefore not allowed access.
我尝试使用 JSONP,但出现语法错误。我很困惑为什么会这样。
$http.jsonp('http://catfacts-api.appspot.com/api/facts?number=1&callback=JSON_CALLBACK').success(function(data) {
console.log(data);
})
.
Uncaught SyntaxError: Unexpected token : facts?number=1&callback=angular.callbacks._0:1
您可以查看页面here
【问题讨论】: