我正在用 javascript 创建一个 websocket 升级请求,需要添加一个 cookie 来添加身份验证 token ,跨域限制是否可行?我的实现非常基础:
document.cookie="token="+authToken+";domain=www.test.com;path=/";
websocket = new WebSocket(endpoint);
最佳答案
您应该更改设置 cookie 值的方式。 无法添加 cookie 的 domain 和 path 值,它们会自动添加。您应该只添加 nameOfProperty 和 valueOfProperty。请参阅下面的简单示例:
document.cookie = "FirstProperty=FirstPropertyValue";
document.cookie = "SecondProperty=SecondPropertyValue";
const ws = new WebSocket('ws://localhost:2427/health');
关于Javascript websocket 客户端将 cookie 添加到 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31419441/