Chrome Extension background page returns error Access-Control-Allow-Origin only sometimes (usually on Windows XP)
I'm writing an extension that requests XML content from a server and displays data in a popup/dialog window. I've added the website to my manifest.json permissions like so:
"permissions": [
"tabs",
"cookies",
"http://www.mywebsite.com/*"
],
Later I added the following code to my background page:
function loadData() {
var url = "http://www.mywebsite.com/api/data.xml";
var xhr = new XMLHttpReque开发者_开发问答st();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// do something
// ....
// save data
DATA.xml = xhr;
} else {
// try to capture error and retry
DATA.error = true;
var pollInterval = 3000;
if (DATA.t) { window.clearTimeout(DATA.t); }
DATA.t = window.setTimeout(loadData, pollInterval);
}
}
}
xhr.send();
}
function init() {
// do something
loadData();
}
<body onload="init()">
</body>
What is strange that during the first usage (after install) on a windows 7 machine this (almost) always succeeds and brings data, but on a windows xp machine (we've tried a few) this (almost) always fails and gives the error:
XMLHttpRequest cannot load http://www.mywebsite.com/api/data.xml. Origin chrome-extension://ficdjnjlmbnjlgdimegfgbakktfnilnp is not allowed by Access-Control-Allow-Origin.
What else, on the xp machine if I click the icon that pops the dialog or options pages (which do not perform any xml requests) then the content is received correctly! As you can see in the code abode I tried re-trying the request multiple times - it always fails with same error, also I tried to delay the initial request to begin after 10 seconds.
Both operating systems are of the same chrome version: Chrome/13.0.782.220 - which should support cross-site xhr requests.
I hope someone can help me with this strange issue...
The short answer is: it's a bug
The long answer: it's being worked on by chromium devs, only seems to occur in special scenarios (probably has to do with another extension/s installed that collides with the one trying to install).
Details here: http://code.google.com/p/chromium/issues/detail?id=88373
精彩评论