我有一个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";
但这没有帮助。
我在本地主机上工作,我看到默认情况下允许本地主机。我也尝试将127.0.0.1添加到列表中,但这也无济于事。
我想念什么?如何使该请求生效?
如果收到400错误,则可能是因为您从API本身收到错误:
常见错误代码
EMAIL_EXISTS:该电子邮件地址已被另一个帐户使用。
OPERATION_NOT_ALLOWED:此项目已禁用密码登录。
TOO_MANY_ATTEMPTS_TRY_LATER:由于异常活动,我们已阻止了来自此设备的所有请求。稍后再试。
实际上,这些错误返回的HTTP状态代码为400。
您可以EMAIL_EXISTS
通过对axios执行以下操作来查看确切的响应消息(例如):
axios.post('/accounts:signUp?key=my_key_goes_here', {
email: formData.email,
password: formData.password,
returnSecureToken: true
})
.then(res => {
console.info(res);
})
.catch(error => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
} else if (error.request) {
console.log(error.request);
} else {
console.log("Error", error.message);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句