Asked  7 Months ago    Answers:  5   Viewed   71 times

When trying to do a HTTP request using XMLHttpRequest from a local file, it basically fails due to Access-Control-Allow-Origin violation.

However, I'm using the local web page myself, so I was wondering if there is any way to make Google Chrome allow these requests, which are from a local file to a URL on the Internet.

E.g., $.get('http://www.google.com/') fails when executing in a local file, but I've scripted the page myself and I'm using it myself, so it would be extremely useful if I could suppress it and load the URL.

So, how can I allow Google Chrome to load URLs using XMLHttpRequest from local files?

 Answers

89

startup chrome with --disable-web-security

On Windows:

chrome.exe --disable-web-security

On Mac:

open /Applications/Google Chrome.app/ --args --disable-web-security

This will allow for cross-domain requests.
I'm not aware of if this also works for local files, but let us know !

And mention, this does exactly what you expect, it disables the web security, so be careful with it.

Tuesday, June 1, 2021
 
CAMason
answered 7 Months ago
52

Nope- can't do this with XHR. Same-domain policy is very restrictive there- same host, same port, same protocol. Sorry! You'll have to resort to other tricks (iframes, title manipulation, etc) to get it to work.

Thursday, August 5, 2021
 
jenny
answered 4 Months ago
92

You can't pass DOM nodes with Messaging; data must be JSON-serializable, and DOM nodes are not.

Pass only data you really need (e.g. textContent), then reconstruct the node on the other side.

Friday, August 6, 2021
 
learningpython
answered 4 Months ago
70

Of course you can! Just use load with an output argument.

x = 1;
save foo;

ls = load('foo.mat');
ls.x
Monday, August 23, 2021
 
OneNoOne
answered 4 Months ago
35

You didn't include the document ready handler, try this:

<script>
    $(function() {
        $("div").css('background-color', 'black');

        $('.click').click(function() {
            chrome.tabs.executeScript(null,
                {code:"document.body.style.backgroundColor='" + $(this).attr('id') + "'"});
            window.close();
        })
    });
</script>
<div class="click" id="red">red</div>
<div class="click" id="blue">blue</div>
<div class="click" id="green">green</div>
<div class="click" id="yellow">yellow</div>

Alternatively you can move your <script> tag to just before the </body> tag so that all HTML is loaded before the javascript.

Sunday, November 28, 2021
 
mikelovelyuk
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share