Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross domain ajax fails even for a local file

I have a local html file with an ajax function trying to pull a xml content from x.com. The file when run works only on IE and fails on firefox and safari browsers. Ofcourse this may be because of the Same origin policy. But,I have heard from someone that for scripts loaded using file:// protocol,this same origin policy will not apply. Is it true and if yes, what can be problem with my local html file?

like image 684
chedine Avatar asked Oct 21 '10 14:10

chedine


People also ask

Can you do cross domain ajax?

You can allow Cross Domain Ajax calls to an application by just registering a new filter and then configure it to Allow-Origin : {your domain's} or you can use a wild card “*” to allow the calls from all domains.

What is ajax Crossdomain?

CORS is a mechanism that defines a procedure in which the browser and the web server interact to determine whether to allow a web page to access a resource from different origin. Figure 2. Cross domain ajax request. When you do a cross-origin request, the browser sends Origin header with the current domain value.

How do I create a cross-origin ajax request?

Clicking the "Fetch HTML5 Rocks" button generates an Ajax call (via jQuery's $. ajax method) to http://www.html5rocks.com/en/tutorials/file/xhr2/. We set local JavaScript variable article to the contents of the first article found in the returned response and display the contents on our page.

What would you enable to allow a browser on another site to make an ajax request your API?

Cross-origin resource sharing (or CORS) can be used to make AJAX requests to another domain. We'll look at how to set up CORS on the server in PHP, how to make the request in JavaScript and some considerations. CORS as a concept is broader than just AJAX requests but this is it's main use.


2 Answers

It is indeed applied to local files, treating them all as separate domains (this varies by browser, as you're seeing). For example in Chrome you can launch it with a command line to allow this:

chrome.exe --allow-file-access-from-files
like image 172
Nick Craver Avatar answered Sep 17 '22 16:09

Nick Craver


In Mozilla file uri have even stricter same origin policy restrictions.. https://developer.mozilla.org/En/Same-origin_policy_for_file:_URIs However you can over-ride by asking for permission for global access using:

 if (navigator.userAgent.indexOf("Firefox") != -1) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            } 
            catch (e) {
                alert("Permission UniversalBrowserRead denied -- not running Mozilla?");
            }
        }
like image 32
Sharad Avatar answered Sep 17 '22 16:09

Sharad