由于CORS Access-Control-Origin,请求失败

因此,我有一个向我的API发出PATCH请求的应用程序。当我提出请求时,它失败并显示

Access to fetch at 'https://localhost:3001/wallets/freshtest@test.com' from origin 'https://localhost:3002' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

但是,如果我运行GET请求,则不会发生此问题。这两个请求

工作提取

const promisofData = fetch(url).then(r=>r.json()).then(data => {
    return data[0].balance;
});

无法取得

fetch(url, {
       method: 'PATCH',
       mode: 'cors',
       headers: {'Content-Type': 'application/json'},
       body: JSON.stringify(iMoney)
});

在我的API中,我确保应允许任何来源

app.use((req, res, next)=>{
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers","*");
    if(req.method === 'OPTIONS'){
        res.header('Access-Control-Allow-Methods', 'POST, GET, PATCH');
        return res.status(200).json({});
    }
    next();
});

我在无法正常工作的提取中尝试了几种不同的模式,但是我很难理解为什么GET请求会起作用,但是PATCH请求会失败?

评论