本文介绍了使快递服务器接受CORS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的快递服务器在 http:// localhost:3000 上运行(我称之为这个网络服务器)我有另一个应用程序运行在本地主机:8100(我称之为简单的应用程序)
I have my express server running on localhost:3000 (I call this web-server) I have another application running on localhost:8100 (I call this simply 'app')
当我的应用程序拨打网络服务器时,我收到消息: / p>
When my app makes a call to the webserver I receive the message:
"XMLHTTPReqeust cannot load localhost:3000/auth/facebook. Response to preflight request doesn't pass access control check. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' when the credentials flag is true. Origin 'localhost:81000' is therefore not allowed acecss"此消息显示在浏览器控制台中。
This message shows up in the browser console.
我已经设置在我的节点webserver的中间件中的以下选项
I have setup the following options in the middleware of my node webserver
res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT, POST,DELETE');阅读几个stackoverfow问题后,我还添加了以下内容:
After reading few stackoverfow questions, I also added the following:
res.header('Access-Control-Allow-Origin', 'localhost:8100');但是这不能解决问题。
推荐答案我个人更喜欢 cors 模块。代码很简单:
I personally prefer the cors module. The code is really simple:
var whitelist = [ '0.0.0.0:3000', ]; var corsOptions = { origin: function(origin, callback){ var originIsWhitelisted = whitelist.indexOf(origin) !== -1; callback(null, originIsWhitelisted); }, credentials: true }; app.use(cors(corsOptions));更多推荐
使快递服务器接受CORS请求
发布评论