简体   繁体   English

Chrome扩展程序后台页面有时会返回错误Access-Control-Allow-Origin(通常在Windows XP上)

[英]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. 我正在编写一个扩展,它从服务器请求XML内容并在弹出/对话框窗口中显示数据。 I've added the website to my manifest.json permissions like so: 我已将网站添加到我的manifest.json权限中,如下所示:

"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: 奇怪的是,在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.

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! 还有什么,在xp机器上,如果我点击弹出对话框或选项页面的图标(不执行任何xml请求),则内容被正确接收! 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. 正如您在代码中所看到的那样,我尝试多次重试该请求 - 它总是以相同的错误失败,我也试图在10秒后延迟初始请求。

Both operating systems are of the same chrome version: Chrome/13.0.782.220 - which should support cross-site xhr requests. 两种操作系统都具有相同的chrome版本:Chrome / 13.0.782.220 - 应支持跨站点xhr请求。

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 详情请访问: http//code.google.com/p/chromium/issues/detail?id = 888373

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM