Liz*_*ing 1 javascript node.js express
感谢评论中的 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, function(){
console.log('Listening on Port: ' + port);
});
Run Code Online (Sandbox Code Playgroud)
我在尝试发帖时遇到 Besic 错误。
Access to XMLHttpRequest at 'localhost:3030/api/signin' from origin 'http://localhost:8100' has been blocked by CORS policy:
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
Run Code Online (Sandbox Code Playgroud)
Chrome 不支持 Access-Control-Allow-Origin:'*' 下的 localhost
Access-Control-Allow-Origin:* 与本地主机不匹配
https://bugs.chromium.org/p/chromium/issues/detail?id=67743
尝试使用127.0.0.1而不是 localhost