Uncaught NetworkError:无法在'WorkerGlobalScope'上执行'importScripts'

问题描述 投票:8回答:2

我试图使用importSctipts从Web worker导入json数据,发生以下错误。

Uncaught NetworkError:无法在'WorkerGlobalScope'上执行'importScripts':无法加载(my:用于从服务器获取数据的URL)的脚本。

Web worker代码在这里。我能够从我的web工作线程和主js线程发送基本消息。我想从我的服务器从Web工作线程中获取jsonp数据,然后回复主js线程。

/*web worker js file to fetch json data from server and then return to main javascript thread*/

self.onmessage = function(e)
{
    var curr = setInterval(function()
    {

         var message = e.data;     
            fetchMyTournament(message);

    }, 10000);
}



function fetchMyTournament(userid)
{
    self.postMessage('worker saying hi');


    var url = "(server URL mapping)?callback=processInfo&type=(typeOfArgument)&userId="+userid;



               importScripts(url);
            self.postMessage("After import script");



}
function processInfo(objJSON) 
{

    self.postMessage("Data returned from the server...: " 
                  + JSON.stringify(objJSON));
} 
javascript web-worker
2个回答
0
投票

importScript()必须放在函数之外。对于您的情况,您应该使用fetch(url)。您还应该为每个函数添加async,并以这种方式使用它:

let message = fetchMyTournament(message).then(function(result){return result;});

0
投票

对于我的情况,我是这样导入PouchDB:importScripts("//cdn.jsdelivr.net/pouchdb/5.3.1/pouchdb.min.js");

该网址应以正确的http / https开头。所以改为这解决了问题:importScripts("https://cdn.jsdelivr.net/pouchdb/5.3.1/pouchdb.min.js");

© www.soinside.com 2019 - 2024. All rights reserved.