在使用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);
}
控制台错误输出文章来源:https://www.toymoban.com/news/detail-702874.html
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:;文章来源地址https://www.toymoban.com/news/detail-702874.html
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; connect-src 'self' ws:;">
</head>```
到了这里,关于使用js连接websocket报错VM4143:11 Refused to connect to ‘‘ because it violates the following Content Securi的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!