Skip to content

Instantly share code, notes, and snippets.

@jtyjty99999
Created August 11, 2014 03:49
Show Gist options
  • Save jtyjty99999/a730a17258fca04bfca3 to your computer and use it in GitHub Desktop.
Save jtyjty99999/a730a17258fca04bfca3 to your computer and use it in GitHub Desktop.
跨域webworker
function XHRWorker(url, ready, scope) {
var oReq = new XMLHttpRequest();
oReq.addEventListener('load', function() {
var worker = new Worker(window.URL.createObjectURL(new Blob([this.responseText])));
if (ready) {
ready.call(scope, worker);
}
}, oReq);
oReq.open("get", url, true);
oReq.send();
}
function WorkerStart() {
XHRWorker("http://static.xxx.com/js/worker.js", function(worker) {
worker.postMessage("hello world");
worker.onmessage = function(e) {
console.log(e.data);
}
}, this);
}
WorkerStart();
@LDY681
Copy link

LDY681 commented Jan 16, 2020

Hi there, I have tried your method but chrome responds with Access to XMLHttpRequest at 'xxx' from origin 'xx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Please let me know how to solve it. Thanks!
BTW The url I am using starts with https, if that is the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment