已解决 无论我如何尝试都无法让 CORS 工作

Liz*_*ing 1 javascript node.js express

感谢评论中的 Freakish解决了localhost:3030问题是我的端点正在使用而不是http://localhost:3030

Node 运行在端口 3030 上,ionic 运行在端口 8100 上,所以我知道这是问题所在localhost:3030localhost: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)