I am trying to import json data from web worker using importSctipts, the following error occurs.
Uncaught NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at (my:URL to fetch data from server) failed to load.
Web worker code is here. I am able to send basic messages from my web worker thread and main js thread. I want to fetch jsonp data from my server from web worker thread and then reply to main js thread.
/*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)); }
For my case i was importing PouchDB like this: importScripts("//cdn.jsdelivr.net/pouchdb/5.3.1/pouchdb.min.js");
The url should start with proper http/https. So changing to this solved the problem: importScripts("https://cdn.jsdelivr.net/pouchdb/5.3.1/pouchdb.min.js");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With