Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting 'Proxy authentication required' when doing XMLHttpRequest (cross-origin) on Chrome Extension background page script
    primarykey
    data
    text
    <p><strong>Question</strong></p> <p>I am getting a <code>Error 407 'Proxy authentication required</code>' when doing a cross-origin XMLHttpRequest from the background page of my Google Chrome extension.</p> <p>I have Chrome set to use a HTTP Proxy which requires a username and password (Basic Auth).</p> <p>The code below works if I do not have a proxy set in Chrome (as expected).</p> <p>When I first open Chrome it asks for my proxy credentials, which I enter, and it remembers for the rest of the session.</p> <p>I am assuming that the extension is in its own process and therefore does not have the credentials to the proxy stored.</p> <p>My question is, is there any way around this? Is there a way to make the XMLHttpRequest work in the extension's background page script no matter what the proxy settings are set to in Chrome browser?</p> <p>I am launching chrome with <code>chrome.exe --proxy-server=my.proxy.server:3128</code> on Windows if that is relevant.</p> <p><strong>Example Code</strong></p> <p>manifest.json</p> <pre><code>{ [...] "background_page": "background.html", "permissions": [ "http://example.org/", "http://mysite.com/" ], "content_scripts": [ { "matches": [ "http://example.org/*" ], "js": ["contentscript.js"] } ] } </code></pre> <p>background.html->background.js:</p> <pre><code>function getById(request, sender, sendResponse) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(data) { if (xhr.readyState == 4) { if (xhr.status == 200) { sendResponse(JSON.parse(xhr.responseText)); } else { // xhr.status == 407 here sendResponse(null); } } } var url = 'http://mysite.com/script.php?id=' + request.id; xhr.open('GET', url, true); xhr.send(); } chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { if ( request.action == 'getById' ) { getById(request, sender, sendResponse); } else { sendResponse(null); } }); </code></pre> <p>contentscript.js:</p> <pre><code>chrome.extension.sendRequest({ 'action': 'getById', 'id': 42 }, function(response) { alert(response) }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload