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.
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. Inspecting the view for the background page shows the console error net::ERR_CONNECTION_REFUSED
when those connections are attempted.
I tried closing all instances of Chrome and then relaunching using command open -a Google\\ Chrome --args --disable-web-security
but still not working.
I tried the CORS
Chrome extension so I could at least test on local server, but that didn't work.
Tried prefixing my live api.example.com
URL with https://www.corsproxy.com/
but the request never completes.
Tried using cors-anywhere.herokuapp.com
prefix but I get back the error origin header required
. To fix that I tried sending the origin header using xhr.setRequestHeader('Origin', http + '//' + window.location.host);
but Chrome does not allow me to proceed with error Refused to set unsafe header "Origin"
.
I tried adding the following response to my server's Laravel controller method, but did not help:
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);
});
If I change the URL from testbox.dev
to my production URL api.example.com
I still get the same cross origin denial.
Any ideas what is wrong here?
net::ERR_CONNECTION_REFUSED
is not a Cross Origin error. You are probably having some issue with your https connection. You can either make sure that you have a proper certificate on your server or switch to 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:
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.