跨域Web Worker? [英] Cross Domain Web Worker?

查看:212
本文介绍了跨域Web Worker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 https://domain1.com (domain1)和

I have https://domain1.com (domain1) and https://domain2.com (domain2).

Domain2提供了一个包含带有此标头的javascript的页面:

Domain2 serves a page containing javascript with this header:

"Access-Control-Allow-Origin: *"

Domain1运行一些可调用的javascript代码:

Domain1 runs some javascript code that invokes:

new Worker("//domain2.com/script.js")

浏览器抛出安全异常.

自从开始写这个问题以来,我已经解决了这个问题,通过取消脚本,对其进行破坏并从中运行它来解决问题,但是我在最初的想法中遗漏了什么吗?

Since starting writing this question, I have got around this problem by ajaxing the script, blobbing it and running it from that, but am I missing something in the original idea?

推荐答案

我也有同样的问题,希望这可以帮助您 https://gist.github.com/jtyjty99999/a730a17258fca04bfca3

I also have the same problem, hope this can help you https://gist.github.com/jtyjty99999/a730a17258fca04bfca3

 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();

这篇关于跨域Web Worker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆