[英]Disabling same origin policy for Chrome for local domains
I'm trying to do cross origin requests on a local *.dev style domain on my Mac OS 10.10 machine using Chrome 45.0.2454.85 (64-bit) for an extension I am developing. 我正在尝试使用我正在开发的扩展程序Chrome 45.0.2454.85(64位)在Mac OS 10.10计算机上的本地* .dev样式域上进行跨源请求。
I can't get a message through to testbox.dev
because every time I do the following code I get value 0
for status
and responseText
is always empty. 我无法通过
testbox.dev
收到消息,因为每次执行以下代码时, status
值都为0
, responseText
始终为空。 Inspecting the view for the background page shows the console error net::ERR_CONNECTION_REFUSED
when those connections are attempted. 尝试这些连接时,检查后台页面的视图将显示控制台错误
net::ERR_CONNECTION_REFUSED
。
I tried closing all instances of Chrome and then relaunching using command open -a Google\\ Chrome --args --disable-web-security
but still not working. 我尝试关闭所有Chrome实例,然后使用命令
open -a Google\\ Chrome --args --disable-web-security
重新启动,但仍然无法正常工作。
I tried the CORS
Chrome extension so I could at least test on local server, but that didn't work. 我尝试了
CORS
Chrome扩展程序,因此至少可以在本地服务器上进行测试,但这没有用。
Tried prefixing my live api.example.com
URL with https://www.corsproxy.com/
but the request never completes. 尝试给我的实时
api.example.com
URL加上https://www.corsproxy.com/
作为前缀,但请求从未完成。
Tried using cors-anywhere.herokuapp.com
prefix but I get back the error origin header required
. 尝试使用
cors-anywhere.herokuapp.com
前缀,但我得到了origin header required
的错误origin header required
。 To fix that I tried sending the origin header using xhr.setRequestHeader('Origin', http + '//' + window.location.host);
为了解决这个问题,我尝试使用
xhr.setRequestHeader('Origin', http + '//' + window.location.host);
发送原始标头xhr.setRequestHeader('Origin', http + '//' + window.location.host);
but Chrome does not allow me to proceed with error Refused to set unsafe header "Origin"
. 但Chrome不允许我继续执行“
Refused to set unsafe header "Origin"
错误Refused to set unsafe header "Origin"
。
I tried adding the following response to my server's Laravel controller method, but did not help: 我尝试将以下响应添加到服务器的Laravel控制器方法,但没有帮助:
return Response::json($stock, 200, ['Access-Control-Allow-Origin' => '*']);
manifest.json: 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 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 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 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);
});
If I change the URL from testbox.dev
to my production URL api.example.com
I still get the same cross origin denial. 如果将URL从
testbox.dev
更改为生产URL api.example.com
我仍然会得到相同的跨源拒绝。
Any ideas what is wrong here? 有什么主意在这里吗?
net::ERR_CONNECTION_REFUSED
is not a Cross Origin error. net::ERR_CONNECTION_REFUSED
不是跨源错误。 You are probably having some issue with your https connection. 您的https连接可能有问题。 You can either make sure that you have a proper certificate on your server or switch to http.
您可以确保服务器上具有正确的证书,也可以切换到http。
Note that the following line: 请注意以下行:
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
Doesn't make much sense in a background page, as the protocol will always be chrome-extension:
. 在后台页面中没有多大意义,因为该协议始终是
chrome-extension:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.