感谢评论中的 Freakish解决了localhost:3030问题是我的端点正在使用而不是http://localhost:3030
Node 运行在端口 3030 上,ionic 运行在端口 8100 上,所以我知道这是问题所在localhost:3030,localhost:8100并且已经寻找了所有实现和允许 CORS 的方法。
我已经尝试了一切,从安装和使用 cors 中间件到遵循其他实施 CORS 的指南。在下面的代码中,我尝试按照指南制作自定义中间件,但即使这样也不起作用。
旁注 socket.io 工作得很好。
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json({ limit: '5mb' }));
// enable CORS
app.use(function( req, res, next ) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "x-requested-with, content-type");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Max-Age", "1000000000");
if ('OPTIONS' == req.method) { res.send(200); } else { next(); }
});
io.on('connection', (socket) => {
socketIo.process(socket, io);
});
app.post('*', (req, res) => {
post.process(req, res);
});
http.listen(port, …Run Code Online (Sandbox Code Playgroud)