Javascript AngularJS:请求的资源上不存在“Access-Control-Allow-Origin”标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29547003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS: No "Access-Control-Allow-Origin" header is present on the requested resource
提问by Ragnarr
I'm writting my webApp and I'm using AngularJS. In this app I have created a file called script.js and I report this code:
我正在编写我的 webApp,我正在使用 AngularJS。在这个应用程序中,我创建了一个名为 script.js 的文件,并报告了以下代码:
var modulo = angular.module('progetto', ['ngRoute']);
// configure our routes
modulo.config(function ($routeProvider, $httpProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'listaFilm.html',
controller: 'listaController'
})
// route for the description page
.when('/:phoneName', {
templateUrl: 'description.html',
controller: 'descriptionController'
});
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
});
modulo.controller('listaController', function ($scope, $http) {
$http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(function (data) {
$scope.names = data;
}).
error(function (data, status) {
$scope.names = "Request failed";
});
});
With this code I call API following RESTful principles. When I run the code i have this problem:
使用此代码,我按照 RESTful 原则调用 API。当我运行代码时,我遇到了这个问题:
XMLHttpRequest cannot load https://api.getevents.coNo 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access.
XMLHttpRequest 无法加载https://api.getevents.co请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8383'。
Reading on the web I understood that I have a problem called CORS...I have tried several solutions proposed but I didn't resolve the problem.
How can I fix the problem?
What's the code that I must add for fix it?
在网上阅读我明白我有一个叫做 CORS 的问题......我已经尝试了几种提出的解决方案,但我没有解决这个问题。
我该如何解决这个问题?
我必须添加什么代码才能修复它?
回答by Kristjan Liiva
This is a server side issue. You don't need to add any headers in angular for cors. You need to add header on the server side:
这是服务器端问题。您不需要为 cors 以角度添加任何标题。您需要在服务器端添加标头:
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
First two answers here: How to enable CORS in AngularJs
这里的前两个答案:如何在 AngularJs 中启用 CORS
回答by Ritesh
CORS is Cross Origin Resource Sharing, you get this error if you are trying to access from one domain to another domain.
CORS 是跨域资源共享,如果您尝试从一个域访问另一个域,则会出现此错误。
Try using JSONP. In your case, JSONP should work fine because it only uses the GET method.
尝试使用 JSONP。在您的情况下,JSONP 应该可以正常工作,因为它只使用 GET 方法。
Try something like this:
尝试这样的事情:
var url = "https://api.getevents.co/event?&lat=41.904196&lng=12.465974";
$http({
method: 'JSONP',
url: url
}).
success(function(status) {
//your code when success
}).
error(function(status) {
//your code when fails
});

