[英]SOLVED Cannot get CORS to work no matter what I try
SOLVED thanks to Freakish in the comments the issue was my endpoint was using localhost:3030
instead of http://localhost:3030
解决了感谢Freakish的问题,这是我的端点正在使用
localhost:3030
而不是http://localhost:3030
Node is running on port 3030 and ionic is running in serve on port 8100 so localhost:3030
and localhost:8100
I know this is the problem and have looked for all the ways to implement and allow CORS. 节点在端口3030上运行,而离子在端口8100上的服务上运行,因此
localhost:3030
和localhost:8100
我知道这是问题所在,并已寻找实现和允许CORS的所有方法。
I have tried everything under the sun from installing and using cors middleware to following other guides on implementing CORS. 从安装和使用cors中间件到遵循有关实现CORS的其他指南,我已经在阳光下进行了所有尝试。 In the code below is where I tried following a guide to make a custom middleware and even that doesn't work.
在下面的代码中,我尝试按照指南制作自定义中间件,即使这样也不起作用。
Side note socket.io is working just fine. 旁注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);
});
Besic error I get when trying to post. 我在尝试发布时遇到基本错误。
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.
I had the same issue with express
and a reactJs
application. 我对
express
和reactJs
应用程序有同样的问题。 To solve it, I added the cors library. 为了解决这个问题,我添加了cors库。
Example with cors on express: 带有cors on express的示例:
const cors = require("cors");
const app = express();
app.use(
cors({
allowedHeaders: ["authorization", "Content-Type"], // you can change the headers
exposedHeaders: ["authorization"], // you can change the headers
origin: "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
preflightContinue: false
});
);
Chrome doesn't support localhost under Access-Control-Allow-Origin:'*' Chrome在Access-Control-Allow-Origin下不支持localhost:'*'
Access-Control-Allow-Origin: * doesn't match localhost Access-Control-Allow-Origin:*与localhost不匹配
https://bugs.chromium.org/p/chromium/issues/detail?id=67743 https://bugs.chromium.org/p/chromium/issues/detail?id=67743
Try, 127.0.0.1 instead of localhost 试试127.0.0.1代替localhost
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.