在使用js连接websocket后发现始终报错
var ws = new WebSocket("ws://localhost:8080");
ws.onopen = function(){
console.log("open");
}
ws.onmessage = function(e){
console.log(e.data);
}
ws.onclose = function(e){
console.log("close");
}
ws.onerror = function(e){
console.log(error);
}
控制台错误输出
Refused to connect to 'ws://127.0.0.1:9394/ws' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
在百度搜索无果后在一系列查略测试后发现在html头中调整加入 connect-src ‘self’ ws:;
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; connect-src 'self' ws:;">
</head>```