为什么Axios会在我的POST请求中发送额外的204 preflight请求?

4xy9mtcn  于 2024-01-07  发布在  iOS
关注(0)|答案(1)|浏览(273)

每当我使用Vue.js(3.x)发送一个POST请求时,都会对同一个URL发出一个额外的请求,HTTP状态码为204,类型为“preflight”。

此预检请求是什么?我如何修复它,使其不会作为副本发送?

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];
        });
    }
},

字符串


的数据

s4chpxco

s4chpxco1#

这不是问题,由浏览器通过设计控制。
这不是Axios或任何其他HTTP客户端决定发送的内容。
preflight request是一个CORS OPTIONS请求,由浏览器自动发送,专门用于检查服务器是否支持您试图在methodheadersorigin方面进行的调用。
如果请求没有失败,您可以安全地忽略这些请求,因为这意味着服务器不会根据上述因素拒绝您的请求。
您的问题与端点不存在有关,因为您正在进行404 Not Found error-检查以查看端点是否存在或您是否正确调用它。

相关问题