5

How I can deal with the CORS policy in Web Worker? I would like to make an HTTPS call in a worker, but it throws the following CORS exception:

XMLHttpRequest cannot load xxxx Origin localhost is not allowed by Access-Control-Allow-Origin.

Maybe I should mention that I'm testing it on a Safari browser

Masood Moshref
  • 370
  • 4
  • 9

3 Answers3

0

I can't comment so I add my 2 cents in "answer". It looks like you haven't configured the CORS headers, you can follow the great tutorial here: http://www.html5rocks.com/en/tutorials/cors/

BR, Saar

Saar
  • 2,276
  • 1
  • 16
  • 14
  • I've already checked and yes it works, but not in a worker and that's the problem. Does anyone have this perhaps a solution? – Masood Moshref Aug 26 '15 at 11:20
  • @MasoodMoshref Have you verified that remote server/domain is returning ACAO header and still it is not working in WebWorker? – Sameer Naik Aug 30 '15 at 22:29
  • See if this is useful http://stackoverflow.com/questions/25458104/can-should-html5-web-workers-use-cors-for-cross-origin – Sameer Naik Aug 30 '15 at 22:32
0

With Web Workers, you can inject scripts with importScripts(). If your server supports JSONP, you could directly make a request like this:

importScripts('http://example.com?jsonp=HandleResponse');

function HandleResponse(data) {
  // Do something with the data
}
George Kormaris
  • 786
  • 8
  • 14
0

It depends on your web server type. E.g. in Express.js (node.js) you make it so:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

For other web servers see this site: http://enable-cors.org/server.html

Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29