在不应该发送印前检查时向服务器发送预检

Sending a Preflight to server when it shouldn't

提问人:L1nK 提问时间:11/15/2023 更新时间:11/15/2023 访问量:23

问:

我的站点出现问题,它正在向服务器发送预检,而不应该发送预检 这是造成问题的函数


    function getInfo(type,end,message,header){
            return new Promise((resolve, reject) => {
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (this.readyState == 4) {
                        if (this.status == 200) {
                            let response = JSON.parse(this.responseText);
                            resolve({ response });
                        } else {
                            reject(new Error(`Falha a obter dados`));
                        }
                    }
                };
            xhttp.open(type, "https://magno.di.uevora.pt/tweb/t1/"+end, true);
            xhttp.setRequestHeader("Content-type", header);
            xhttp.send(message);
            });
    }
    function addagendamento(){
        let message=JSON.stringify({
            user_id:3,
            vaccine:"gripe",
            entity_id:3
        });
        getInfo("POST","schedule/add",message,"application/json")
        .then(({ response }) => {    
        
            })
            .catch(error => {
            console.error("Error:", error.message);
        });
    }

服务器端的代码是由教授完成的,我有一个朋友有类似的代码,在他的 PC 上没有收到这个错误,但在我的代码中,错误仍然附加在他的 PC 上,尽管我尝试过在我的 PC 上编写代码并得到相同的错误。 这是错误

Access to XMLHttpRequest at 'https://magno.di.uevora.pt/tweb/t1/schedule/add' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
trabalho.js:339 Error: Falha a obter dados
(anônimo) @ trabalho.js:339
Promise.catch (assíncrono)
addagendamento @ trabalho.js:338
onclick @ test.html?idU=3:76
trabalho.js:325 
POST https://magno.di.uevora.pt/tweb/t1/schedule/add net::ERR_FAILED

根据我的研究,它与向服务器发送预留有关,它不能很好地处理它,但它甚至不应该发送预留。(我认为)

JavaScript HTML Web 浏览器 CORS

评论

0赞 jub0bs 11/15/2023
问题的前提是不正确的:根据 Fetch 标准,包含值为 CORS 预检的标头的跨域请求。Content-Typeapplication/json

答: 暂无答案