Chrome 扩展后台页面仅有时返回错误 Access-Control-Allow-Origin(通常在 Windows XP 上)
我正在编写一个扩展,它从服务器请求 XML 内容并在弹出/对话框窗口中显示数据。我已将该网站添加到我的 manifest.json 权限中,如下所示:
"permissions": [
"tabs",
"cookies",
"http://www.mywebsite.com/*"
],
后来我将以下代码添加到我的后台页面:
function loadData() {
var url = "http://www.mywebsite.com/api/data.xml";
var xhr = new XMLHttpRequest();
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>
奇怪的是,在 Windows 7 计算机上第一次使用(安装后)期间,这(几乎)总是 成功并带来数据,但是在Windows XP机器上(我们已经尝试了一些),这(几乎)总是失败并给出错误:
XMLHttpRequest cannot load http://www.mywebsite.com/api/data.xml. Origin chrome-extension://ficdjnjlmbnjlgdimegfgbakktfnilnp is not allowed by Access-Control-Allow-Origin.
还有什么,在xp机器上如果我单击弹出对话框的图标或选项页面(不执行任何 xml 请求),则内容被正确接收!正如您在代码中所看到的,我尝试多次重试该请求 - 它总是因相同的错误而失败,而且我还尝试将初始请求延迟 10 秒后开始。
两个操作系统都具有相同的 chrome 版本:Chrome/13.0.782.220 - 应该支持跨站点 xhr 请求。
我希望有人能帮助我解决这个奇怪的问题......
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 XMLHttpRequest();
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是:这是一个错误
长的答案:它正在由 chromium 开发人员处理,似乎只发生在特殊场景中(可能与安装的另一个扩展与尝试安装的扩展发生冲突有关)。
详细信息请参见:http://code.google.com/p/chromium/问题/详细信息?id=88373
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