XMLHttpRequest and Web Workers Access-Control-Origin

I'm dealing with some Access-Control-Origin issues when using webworkers to make an XMLHttpRequest. The issue is easily reproducible in this code, main.js: var worker = new Worker('js/createSimplePage.js'); worker.onmessage = function () { console.log('message recieved'); }; And js/createSimplePage.js: var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { console.log(xmlhttp.responseText); } }; xmlhttp.open("GET", "http://search.twitter.com/search.json?q=balling&rpp=20&callback=", true); xmlhttp.setRequestHeader('Content-Type', 'text/plain'); xmlhttp.send(); If I include createSimplePage.js in the header of index.html, it runs fine and prints the correct response from the twitter API. However, if I only load main.js and let it try to load createSimplePage.js as a web worker then I receive the error: XMLHttpRequest cannot load http://search.twitter.com/search.json?q=balling&rpp=20&callback=. Origin file:// is not allowed by Access-Control-Allow-Origin. A similar error occurs when hosting the files on SimpleHTTPServer except the error is then "Origin http://127.0.0.1:8000 is not allowed by Access-Countrol-Allow-Origin." I don't understand why loading it as a web-worker would break it so it can't access the API.

以上就是XMLHttpRequest and Web Workers Access-Control-Origin的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏