每当我使用 Vue.js (3.x) 发送
POST
请求时,都会使用 HTTP 状态代码 204 和“预检”类型向同一 URL 发出额外请求。
此预检请求是什么?如何修复它,使其不会重复发送?
Register.vue
async submit() {
this.button = true;
try {
const response = await axios.post(`register`, this.form);
(response.data.success == false)
? console.log(response.data.message)
: this.$router.push('/');
} catch (error) {
let { errors } = error.response.data;
this.button = false;
this.errors = {};
Object.keys(errors).forEach(element => {
this.errors[element] = errors[element][0];
});
}
},
这不是问题,由浏览器设计控制。
这不是 Axios 或任何其他 HTTP 客户端决定发送的内容。
预检请求是一个 CORS
OPTIONS
请求,由浏览器自动发送,专门用于检查服务器是否支持您尝试在method、headers和origin方面进行的调用。
如果请求没有失败,您可以安全地忽略它们,因为这意味着服务器不会根据上述因素拒绝您的请求。
您的问题与端点不存在有关,因为您收到 404 Not Found 错误 - 检查端点是否存在或您是否正确调用它。