1 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
問題可能是您鏈接到的后續(xù)路由next沒有意識(shí)到它們正在處理OPTIONS請(qǐng)求(預(yù)檢)。對(duì)預(yù)檢的響應(yīng)應(yīng)該只是標(biāo)題。
如果是這樣,您可以像這樣修復(fù)它:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:3000") // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
if (req.method === "OPTIONS") {
res.status(200).end();
} else {
next()
}
});
...前提是中間件優(yōu)先于其他中間件,等等。
也就是說,您可能會(huì)查看一個(gè)久經(jīng)考驗(yàn)的中間件,而不是自己動(dòng)手做。cors似乎很受歡迎。
添加回答
舉報(bào)