为本地域的Chrome禁用相同的原始策略

时间:2015-09-12 00:38:27

标签: javascript google-chrome google-chrome-extension xmlhttprequest cross-domain

我正在尝试使用Chrome 45.0.2454.85(64位)在Mac OS 10.10计算机上的本地* .dev样式域上进行跨源请求,以用于我正在开发的扩展程序。

我无法收到testbox.dev的消息,因为每次执行以下代码时,0的值为status,而responseText始终为空。检查后台页面的视图会在尝试连接时显示控制台错误net::ERR_CONNECTION_REFUSED

我尝试关闭Chrome的所有实例,然后使用命令open -a Google\ Chrome --args --disable-web-security重新启动,但仍无效。

我尝试了CORS Chrome扩展程序,因此我至少可以在本地服务器上进行测试,但这不起作用。

尝试使用api.example.com为我的实时https://www.corsproxy.com/网址加前缀,但请求永远不会完成。

尝试使用cors-anywhere.herokuapp.com前缀但我收回错误origin header required。要解决此问题,我尝试使用xhr.setRequestHeader('Origin', http + '//' + window.location.host);发送原始标头,但Chrome不允许我继续执行错误Refused to set unsafe header "Origin"

我尝试将以下响应添加到我的服务器的Laravel控制器方法中,但没有帮助:

return Response::json($stock, 200, ['Access-Control-Allow-Origin' => '*']);

的manifest.json:

{
    "name": "__MSG_appName__",
    "version": "1.0.0",
    "manifest_version": 2,
    "description": "__MSG_appDescription__",
    "icons": {
        "16": "images/icon-16.png",
        "48": "images/icon-48.png",
        "128": "images/icon-128.png"
    },
    "default_locale": "en",
    "background": {
        "scripts": [
            //"scripts/chromereload.js"
            "scripts/background.js"
        ],
        "persistent": false
    },
    "browser_action": {
        "default_icon": {
            "16": "images/icon-16.png",
            "32": "images/icon-32.png",
            "38": "images/icon-38.png",
            "48": "images/icon-48.png",
            "64": "images/icon-64.png",
            "128": "images/icon-128.png"
        },
        "default_title": "Workflow Enhancer"
    },
    "options_page": "options.html",
    "content_scripts": [
        {
            "matches": [
                "http://www.example.com/*",
                "https://www.example.com/*",
                "https://*.freshbooks.com/*",
                "https://*.highrisehq.com/*"
            ],
            "css": [
                "styles/content.css"
            ],
            "js": [
                "scripts/jquery.min.js",
                "scripts/xhrproxy.js",
                "scripts/content.js"
            ],
            "run_at": "document_end",
            "all_frames": false
        }
    ],
    "permissions": [
        "activeTab",
        "<all_urls>",
        "http://*.dev/*",
        "https://*.dev/*",
        "http://testbox.dev/*",
        "https://testbox.dev/*",
        "http://*.example.com/*",
        "https://*.example.com/*"
    ],
    "web_accessible_resources": [
        "*"
    ]
}

background.js

chrome.extension.onConnect.addListener(function(port) {
    if (port.name != 'XHRProxy_')
        return;

    port.onMessage.addListener(function(xhrOptions) {
        var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
        var xhr = new XMLHttpRequest();

        xhr.open(xhrOptions.method || "GET", http + xhrOptions.url, true);
        //xhr.setRequestHeader('Origin', http + '//' + window.location.host);
        xhr.setRequestHeader('X-Requested-With', 'XHRProxy');
        xhr.setRequestHeader('X-API-key', 'JSFLIESLIFDFDHSLFEHSLFHSFH');

        xhr.onreadystatechange = function() {
            if (this.readyState == 4) {
                port.postMessage({
                    status : this.status,
                    data   : this.responseText,
                    xhr    : this
                });
            }
        };

        xhr.send();
    });
});

xhrproxy.js

var proxyXHR = {};

proxyXHR.get = function (url) {
    var port     = chrome.extension.connect({ name: 'XHRProxy_' });
    var settings = {
        method : 'GET',
        url    : url
    };
    var onSuccess;
    var onFailure;
    var self = {
        onSuccess: function (callback) {
            onSuccess = callback;
            return self;
        },
        onFailure: function (callback) {
            onFailure = callback;
            return self;
        }
    };
    port.onMessage.addListener(function (msg) {
        if (msg.status === 200 && typeof onSuccess === 'function') {
            onSuccess(msg.data, msg.xhr);
        } else if (typeof onFailure === 'function') {
            onFailure(msg.data, msg.xhr);
        }
    });
    port.postMessage(settings);
    return self;
};

content.js

// Localhost test domain.
proxyXHR.get('testbox.dev/api/XYZ/quantity')
            .onSuccess(function (data) {
                console.log(data);
            })
            .onFailure(function (data, xhr) {
                console.log("HTTP Error while retrieving data.", data, xhr.status);
            });

// Production server domain....produces same error as local domain test above.
proxyXHR.get('api.example.com/api/XYZ/quantity')
            .onSuccess(function (data) {
                console.log(data);
            })
            .onFailure(function (data, xhr) {
                console.log("HTTP Error while retrieving data.", data, xhr.status);
            });

如果我将网址从testbox.dev更改为我的生产网址api.example.com,我仍会获得相同的交叉来源拒绝。

这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

net::ERR_CONNECTION_REFUSED不是Cross Origin错误。您的https连接可能存在一些问题。您可以确保服务器上有正确的证书,也可以切换到http。

请注意以下一行:

    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');

在后台页面中没有多大意义,因为协议始终为chrome-extension: