javascript 不允许访问控制源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23469747/
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
Access Control Origin not Allowed
提问by user sks
I am facing "Access Control Origin not Allowed" issue in AngularJS and based on AngularJS CORS IssuesI have added following lines in my code:
我在 AngularJS 中面临“不允许访问控制源”问题,并且基于AngularJS CORS 问题,我在代码中添加了以下几行:
angular.module('app', [
'ngRoute',
'appModules.filters',
'appModules.ajaxCallService'
])
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$routeProvider.when('/login', {templateUrl: 'views/login.html', controller: 'LoginCtrl'});
}]);
But still I am facing issue:
但我仍然面临问题:
XMLHttpRequest cannot load http://127.0.0.1:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
XMLHttpRequest cannot load http://127.0.0.1:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
Can anyone help me to solve this issue?
谁能帮我解决这个问题?
Even using localhost in URL I am getting same issue
即使在 URL 中使用 localhost 我也遇到同样的问题
XMLHttpRequest cannot load http://localhost:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
XMLHttpRequest cannot load http://localhost:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
I am using AngularJS version 1.2.16 and expressJS with 0.10.26 nodejs.
我正在使用 AngularJS 版本 1.2.16 和 expressJS 与 0.10.26 nodejs。
回答by user sks
My problem got resolved i have added
我的问题得到了解决我已经添加
res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
in my expressJS code while sending data to client applicatin.
在我的 expressJS 代码中,同时将数据发送到客户端应用程序。