预检响应中不允许使用方法选项
Posted
技术标签:
【中文标题】预检响应中不允许使用方法选项【英文标题】:Method Options is not allowed in preflight response 【发布时间】:2020-10-10 22:47:48 【问题描述】:由于 cors 限制,我无法将内容类型为 application/json 的帖子请求发送到我的后端。 我已经开始使用“cors”模块,还为这些路线启用了飞行前请求。
我的请求现在将得到正确答复和处理,但我的控制台上仍然会出现以下错误,我不确定它是否有我不知道的副作用。
Access-Control-Allow-Methods 不允许方法 OPTIONS 预检响应。
选项https://example.com/api/postRequest 网络::ERR_FAILED
const cors = require('cors');
const corsOptions =
origin: 'https://example.com',
optionsSuccessStatus: 200,
;
app.options('/api/postRequest', cors(corsOptions), function (req, res, next)
res.header("Access-Control-Allow-Methods", "*");
res.setHeader('Content-Type', 'application/json');
next()
)
app.post('/api/postRequest', cors(corsOptions), async (req, res) =>
res.header("Access-Control-Allow-Methods", "*");
res.setHeader('Content-Type', 'application/json');
//do other stuff and send response
【问题讨论】:
【参考方案1】:要启用所有 http 方法,请使用:
const corsOptions =
origin: 'https://example.com',
optionsSuccessStatus: 200,
methods: '*'
;
【讨论】:
以上是关于预检响应中不允许使用方法选项的主要内容,如果未能解决你的问题,请参考以下文章