繁体   English   中英

Chrome中的错误:“ Access-Control-Allow-Origin不允许原始文件://”

[英]Error in Chrome: “Origin file:// is not allowed by Access-Control-Allow-Origin”

我正在为需要访问minus.com的Chrome扩展程序编写代码,该扩展程序使用oAuth 2.0进行身份验证,因此我编写了一个javascript文件“ test.js”,然后将其包含在HTML文件“ test.html”中,然后加载Chrome中的“ test.html”以测试用于身份验证的javascript代码。

“ test.js”的结构如下所示:

function Ajax(url, options) {

// some function content

    // Sending data
    if (options.method === "POST" && (options.params || options.binaryData)) {
        if (options.binaryData) {
            xhr.sendAsBinary(options.binaryData);
        } else {
            xhr.send(hashToQueryString(options.params));
        }
    } else {
        xhr.send(null);
    }

    return xhr;
}

function refreshToken(refresh_token) {
    var params = {
        'grant_type': 'refresh_token',
        'client_id': API_KEY,
        'client_secret': API_SECRET,
        'refresh_token': refresh_token,
        'scope': 'read_all modify_all upload_new'
    }

    new Ajax("https://minus.com/oauth/token", {
        params: params,

        onSuccess: function(response) {
            console.log(response.access_token);
        },

        onError: function(response) {
            console.log('error: wrong_token');
        }
    });               
}

refreshToken();

当我在Chrome中加载“ test.html”以测试“ test.js”时,它在控制台中提示错误,并说“ XMLHttpRequest无法加载https ://minus.com/oauth/token?...源文件: // Access-Control-Allow-Origin不允许。” 我尝试使用选项“ --allow-file-access-from-files”或“ --disable-web-security”启动Chrome,但是并不能解决问题。 但是,如果我在“ Ajax”功能中注释了“发送数据”部分,则没有错误。

有人知道这里发生了什么吗?

谢谢!

要具体说明您的问题:您需要将要能够跨域访问的主机添加到扩展清单中:

http://code.google.com/chrome/extensions/xhr.html

{
  "name": "My extension",
  ...
  "permissions": [
    "https://*.minus.com/"
  ],
  ...
}

编辑

附带一提,当我有大量的扩展程序处于活动状态时,有时会在chrome中出现奇怪的跨域错误。 然后,我必须至少禁用几个,刷新扩展名,然后它才能工作-有时重新启动chrome。

您可以通过使您的应用程序成为支持跨域调用的打包应用程序来解决此问题。 托管的应用程序则没有。

您可能还想阅读chrome扩展中的跨域XMLHttpRequest

暂无
暂无

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

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