在本地做一个前后分离的项目,用的egg,出现 OPTIONS /api/posts] nodejs.MethodNotAllowedError: Method Not Allowed
发布于 2 天前 作者 jiyarong 232 次浏览 来自 问答

在本地做一个前后分离的项目,用的egg,出现 OPTIONS /api/posts] nodejs.MethodNotAllowedError: Method Not Allowed 我前端用的fetch api,貌似跨域会默认发送一条options请求,我也在后端项目里面加了

router.options('/api/posts', controller.post.option)

但是却出现OPTIONS /api/posts] nodejs.MethodNotAllowedError: Method Not Allowed的错误

5 回复

跨域用egg-cors来处理,不需要对options请求进行处理

在服务端解决跨域问题就可以了不用特意处理这个options

config/plugin.js exports.cors = { enable: true, package: “egg-cors” };

config/config.default.js config.security= { csrf: { enable: false, ignoreJSON: true }, domainWhiteList: ['http://localhost:xxxx] } config.cors= { allowMethods: “GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS” },

跨域什么的都是能避免就尽量避免,你怎么还主动去找跨域问题去了?我们前后端分离出现跨域都是前端起node服务,绕过跨域问题的。

前端服务器设置反向代理去,开发时react/vue都有相应配置,页面部署到nginx/tomcat也有相应配置

来自酷炫的 CNodeMD

回到顶部