【发布时间】:2020-02-16 05:04:21
【问题描述】:
我有一个 Axios 的实例:
import axios from 'axios';
const instance = axios.create({
baseURL: 'https://identitytoolkit.googleapis.com/v1'
});
export default instance;
然后我将它导入到我的 signup.vue 文件中:
<script>
import axios from '../../axios-auth';
...
</script>
在那个 Vue 文件中,我有一个注册表单,一旦我点击提交按钮,它就会运行以下方法:
onSubmit() {
const formData = {
email: this.email,
age: this.age,
password: this.password,
confirmPassword: this.confirmPassword,
country: this.country,
hobbies: this.hobbyInputs.map(hobby => hobby.value),
terms: this.terms
};
console.log(formData);
axios.post('/accounts:signUp?key=my_key_goes_here', {
email: formData.email,
password: formData.password,
returnSecureToken: true
})
.then(res => {
console.info(res);
})
.catch(error => {
console.error(error);
});
}
我收到 403 错误 - 禁止 400 错误 - 错误请求。
我尝试更改标题:
instance.defaults.headers.post["Access-Control-Allow-Origin"] = "localhost";
instance.defaults.headers.common["Content-Type"] = "application/json";
但这并没有帮助。
我在 localhost 工作,我看到默认情况下允许使用 localhost。我也尝试将 127.0.0.1 添加到列表中,但这也没有帮助。
我错过了什么?我怎样才能使这个请求生效?
【问题讨论】:
-
Access-Control-Allow-Origin 是一个响应头。它必须在服务器端设置,而不是在发出请求的前端 JavaScript 代码中。如果您收到 403 响应,预计它不会有 Access-Control-Allow-Origin 响应标头。通常,服务器只将您的应用程序集标头添加到成功响应中——2xx 和可能的 3xx 响应——而不是 4xx 或 5xx 错误响应。而且您所做的任何 CORS 配置都不会导致服务器响应 403。因此,无论实际导致 403 错误的原因是什么,它都与 CORS 配置无关。
-
@sideshowbarker 我看到一条错误消息,说它是 CORS。由于某种原因,现在我看不到它,但现在我有一个 400 错误 - 错误的请求。我看到请求转到
https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=my_api_key,根据 Firebase 文档,它应该是 API 端点。
标签: firebase firebase-authentication axios