js多线程cannot be accessed from origin 问题,new worker

----

Web Workers 为 Web 前端网页上的脚本提供了一种能在后台进程中运行的方法。一旦它被创建,Web Workers 就可以通过 postMessage 向任务池发送任务请求,执行完之后再通过 postMessage 返回消息给创建者指定的事件处理程序 ( 通过 onmessage 进行捕获 )。more》

----

错误:

Uncaught DOMException: Failed to construct 'Worker': Script at 'http://127.0.0.1/compute.js' cannot be accessed from origin 'http://localhost'.…

Failed to construct 'Worker': Script at 'file:///E:/wamp64/www/compute.js' cannot be accessed from origin 'null'.

可能的原因:

  1. 必须在http/https协议下访问HTML文件,不能用文件协议(类似file:///E:/wamp64/www/t.html 这种)
  2. new Worker(xxx.js)里的xxx.js必须和HTML文件同源(注意:http://127.0.0.1和http://localhost不同源,呵呵。。。)

猜你喜欢

转载自blog.csdn.net/qq_36110571/article/details/103187317