jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.12k stars 269 forks source link

Possible to use xdomain in a web worker? #139

Closed marclar closed 9 years ago

marclar commented 9 years ago

I assume it's not because it seems to rely on iframes / the DOM. Is that correct?

jpillora commented 9 years ago

A web worker is a pure JS environment (no DOM) and XDomain relies on a iframes (DOM). If you need to perform an XDomain proxied request through a web worker, then I think your best option is define an RPC-like function back to the host page, where it does something like:

//this is psuedo-code
worker.on("message", function(e) {
  var data = e.data;
  if(data.ajax) {
    $.ajax(data.ajax).always(function(results) {
       worker.send({ id, data.id, ajax: results });
    });
  }
});
owlabio commented 9 years ago

Hi there !

Many thanx for your API !!!

I'm stuck though with this worker problem.

Could you please elaborate on this RPC-like approach ?

Cheers,