【发布时间】:2016-01-26 05:21:01
【问题描述】:
在我的 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。我不确定我错过了什么?这就是我的本地主机链接的样子 http://localhost/static/manage/v2/#/login
【问题讨论】:
-
查看本页右侧的所有相关问题。他们每个人都是同一个主题。问题每天在这里出现无数次,并不难研究
-
是的,到目前为止,我已经经历了大约 5 个,我觉得没有一个回答我的问题 :( 我看到还有更多,现在正在经历它们
-
您与我们共享的代码都没有设置原始标头,因此我们无法判断为什么它将 Access-Control-Allow-Origin 设置为
http://localhost:8100而不是http://localhost -
重要的是要注意在大多数浏览器中不同的端口被认为是不同的来源
-
不同的端口在所有浏览器中被认为是不同的来源。这就是规范所说的。它不被视为不同的域,因为它不是一个域。
标签: javascript angularjs xmlhttprequest