这是安全的一部分,你不能这样做。如果你想允许凭证,那么你的 Access-Control-Allow-Origin 一定不要用 * 。您必须指定确切的协议+域+端口。供参考,请参阅以下问题:
Access-Control-Allow-Origin
*
除了 * 过于宽容,会破坏凭证的使用。所以设定 http://localhost:3000 要么 http://localhost:8000 作为允许原始标题。
http://localhost:3000
http://localhost:8000
在Chrome中进行开发,安装 这个加上 将摆脱那个特定的错误:
Access to XMLHttpRequest at 'http://192.168.1.42:8080/sockjs-node/info?t=1546163388687' from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
安装后,请确保将您的网址格式添加到 Intercepted URLs 点击AddOn( CORS ,绿色或红色)图标并填写相应的文本框。要在此处添加的示例URL模式 http://localhost:8080 将会: *://*
Intercepted URLs
http://localhost:8080
*://*
试试吧:
const cors = require('cors') const corsOptions = { origin: 'http://localhost:4200', credentials: true, } app.use(cors(corsOptions));
如果您正在使用CORS中间件并且想要发送 withCredential boolean true,你可以像这样配置CORS:
withCredential
var cors = require('cors'); app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
如果你正在使用 express 你可以使用 CORS 允许CORS这样的包,而不是编写你的中间件;
express
var express = require('express') , cors = require('cors') , app = express(); app.use(cors()); app.get(function(req,res){ res.send('hello'); });