【发布时间】:2017-04-13 07:17:15
【问题描述】:
我正在使用:
- Vue 2.0.3
- vue-router 2.0.1
- vuex 0.8.2
- vue-resource 0.7.0
在尝试使用远程 API(而不是本地运行的 API)登录我的页面后,我收到如下所示的 cors 错误
vue-resource.common.js?2f13:1074 OPTIONS
https://mywebsite/api/auth/login
(anonymous function) @ vue-resource.common.js?2f13:1074
Promise$1 @ vue-resource.common.js?2f13:681
xhrClient @ vue-resource.common.js?2f13:1033
Client @ vue-resource.common.js?2f13:1080
(anonymous function) @ vue-resource.common.js?2f13:1008
XMLHttpRequest cannot load https://mywebsite/api/auth/login.
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed
access. The response had HTTP status code 415.
现在我在 Azure 中运行了 API,因为它允许我测试来自 Postman 的调用,所以我很确定 CORS 标头在后端已正确设置。 对 Vue 和前端不太确定。
我在配置文件中有这样的情况:
export const API_ROOT = 'https://mywebsite/api/'
export const AuthResource = Vue.resource(API_ROOT + 'auth{/action}')
比我这样称呼这个动作:
login: function (userData) {
return AuthResource.save({action: 'login'}, userData)
}
最后,当我通过 vuex 子模块中的令牌检查登录中的身份验证时,我有 只是一个简单的头部检查状态。
var updateAuthHeaders = () => {
var token = JSON.parse(localStorage.getItem("auth_token"))
if (token != null){
Vue.http.headers.common['Authorization'] = token
}else{
Vue.http.headers.common['Authorization'] = null
}
}
我尝试在此处添加Vue.http.headers.common['Access-Control-Allow-Origin'] = true,但没有帮助。
有什么想法吗?我做错了什么..我想如果它不适用于登录,它也不适用于其他调用。
【问题讨论】:
-
这似乎不是 CORS 问题(如 415 错误所示),您的客户端代码没有任何问题。您能否提供您的后端密钥代码 sn-p?
-
你完全正确!我的 API 项目存在不允许从浏览器中的 JS 访问的问题
-
@desicne 你能说出你的问题吗?
-
@Darem 不是前端问题。服务器设置当时没有正确完成。
标签: javascript azure vue.js vue-resource