XMLHttpRequest 无法加载对预检请求的响应未通过访问控制检查
XMLHttpRequest cannot load Response to preflight request doesn't pass access control check
在我的 Angular 应用程序中,下面是我在本地开发时尝试登录时遇到的错误 (Chrome):
XMLHttpRequest cannot load https://www.tickertags.com/app/api/login.
Response to preflight request doesn't pass access control check:
The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8100'
that is not equal to the supplied origin.
Origin 'http://localhost' is therefore not allowed access.
我的 loginController 的登录功能
function login(credentials) {
console.log('credentials',credentials);
AuthFactory.login(credentials).then(function (user) {
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
$scope.setCurrentUser(user);
if (user.password_reset) {
$location.path('/password');
} else {
$location.path('/tickers');
}
}, function () {
console.log('"Invalid Username/Password"');
$scope.loginData.message = "Invalid Username/Password";
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
}, function () {
console.log('"Invalid Username/Password"');
$scope.loginData.message = "Invalid Username/Password";
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
});
}
AuthFactory.login
函数命中生产环境中托管的 API。我不确定我错过了什么?这是我的本地主机 link 的样子 http://localhost/static/manage/v2/#/login
要克服 CORS 问题,您必须同时配置 front-end 和 back-end。
对于front-end,在初始化时调用myApp.run()
方法时可以配置默认HTTP headers:
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
对于 back-end 部分,您需要修改请求 headers。我不知道你的 back-end 上有什么,但我通常在我的本地节点服务器上使用它们。
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
希望这可以帮助 运行 遇到此问题但无法访问服务器端任何内容的其他人。
我发现了我的问题,我试图通过我的 API 登录调用找到一条绝对路径,即:www.tickertags.com/api/...
而不是 /api/...
来修复它。
在我的 Angular 应用程序中,下面是我在本地开发时尝试登录时遇到的错误 (Chrome):
XMLHttpRequest cannot load https://www.tickertags.com/app/api/login.
Response to preflight request doesn't pass access control check:
The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8100'
that is not equal to the supplied origin.
Origin 'http://localhost' is therefore not allowed access.
我的 loginController 的登录功能
function login(credentials) {
console.log('credentials',credentials);
AuthFactory.login(credentials).then(function (user) {
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
$scope.setCurrentUser(user);
if (user.password_reset) {
$location.path('/password');
} else {
$location.path('/tickers');
}
}, function () {
console.log('"Invalid Username/Password"');
$scope.loginData.message = "Invalid Username/Password";
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
}, function () {
console.log('"Invalid Username/Password"');
$scope.loginData.message = "Invalid Username/Password";
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
});
}
AuthFactory.login
函数命中生产环境中托管的 API。我不确定我错过了什么?这是我的本地主机 link 的样子 http://localhost/static/manage/v2/#/login
要克服 CORS 问题,您必须同时配置 front-end 和 back-end。
对于front-end,在初始化时调用myApp.run()
方法时可以配置默认HTTP headers:
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
对于 back-end 部分,您需要修改请求 headers。我不知道你的 back-end 上有什么,但我通常在我的本地节点服务器上使用它们。
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
希望这可以帮助 运行 遇到此问题但无法访问服务器端任何内容的其他人。
我发现了我的问题,我试图通过我的 API 登录调用找到一条绝对路径,即:www.tickertags.com/api/...
而不是 /api/...
来修复它。