简体   繁体   English

已解决无论我如何尝试都无法使CORS正常工作

[英]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:3030localhost: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. 我对expressreactJs应用程序有同样的问题。 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.

相关问题 无论我尝试什么,我都无法设置工作间隔 - I cant get set interval to work no matter what I try 无论我怎么尝试,JavaScript 函数都不起作用 - JavaScript function won't work no matter what I try 无论我尝试什么,要求代码都无法工作 - requiring code fails to work no matter what I try 无论我尝试什么,sql都不会返回任何数据 - sql returns no data no matter what I try 无论我尝试什么,都无法在 Angular Typescript 模块中导入 Node、JS 或 Electron 库 - Cannot import Node, JS, or Electron library in Angular Typescript module no matter what I try 无法使XmlHttpRequest与CORS一起使用 - Cannot get XmlHttpRequest to work with CORS 我正在尝试创建一个简单的程序,它将为我提供 2500000 的 23%,无论我如何更改,我都无法使其正常工作。 我正在使用记事本++ - i am trying to create a simple program that will give me 23% of 2500000 I cannot get this to work no matter what i change. I am using notepad++ 无论我尝试什么,引导模式都不会聚焦 - Bootstrap modal won't focus no matter what I try Gatsby.js 问题<link> ,无论我尝试什么 - Gatsby.js problem with <Link>, no matter what I try 我的幻灯片不能居中显示,我尝试的都无所谓 - My Slideshow Does Not Centre, No Matter What I Try
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM