【问题标题】:Set withCredentials attribute for XMLHttpRequest in Socket.io在 Socket.io 中为 XMLHttpRequest 设置 withCredentials 属性
【发布时间】:2017-05-30 19:14:49
【问题描述】:

我在使用 socket.io 时遇到以下错误

XMLHttpRequest cannot load https://******:3000/socket.io/?
EIO=3&transport=polling&t=LPBFdGH. A wildcard '*' cannot be used in the 'Access-Cont
rol-Allow-Origin' header when the credentials flag is true. 
Origin 'https://*****.com' is therefore not allowed 
access. The credentials mode of an XMLHttpRequest is controlled by the 
withCredentials attribute

我已经使用 CORS 将属性设置为无效。

app.use(cors({ 出处:真实, 凭据:真 }));

还有其他方法吗?

【问题讨论】:

    标签: javascript node.js sockets socket.io


    【解决方案1】:
    A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true.
    

    这里的关键点是您的 CORS 配置的 origin:true 部分会为 Access-Control-Allow-Origin 标头生成一个 * 值。在 socket.io 中为 XHR 请求使用 withCredentials 属性时,这是不可接受的。

    您需要明确允许来源,例如这样:

    app.use(cors({ origin: 'https://******.com', credentials: true }));
    

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 2018-05-24
      • 2019-12-08
      • 2019-12-04
      • 1970-01-01
      • 2014-11-29
      • 2012-06-14
      • 1970-01-01
      • 2017-03-31
      相关资源
      最近更新 更多