27

I am working to dynamically create a UI from XML using jQuery. My jQuery is working in Firefox but in Chrome it's not working. It gives me this console error:

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

The following is my jQuery code which working on Firefox but not working on Google chrome:

$.ajax({
    url: 'file:///home/satendra/dndExamples/avisDnD/file.xml',
    success: function(xml) {
        $(xml).find('Tab').each(function() {
            var id = $(this).attr('URL');
            var tab = $(this).attr('TabName');
            $("ul").append("<li><a href="+ id +">"+ tab +"</li>");
        });
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
vibog
  • 283
  • 1
  • 4
  • 8

1 Answers1

30

Firefox allows the request because it accepts requests to the local file system (ie. the file:// protocol) if they originate from there too. However Chrome denies all XMLHttpRequests to file:// urls.

Note that you cannot make an AJAX request to the local file system from an external domain in either browser - it would be a massive security flaw if you could.

For this AJAX request to work in Chrome you need to make the request to a webserver. If you're on Windows you can easily install IIS or WAMP on your local machine.

Note that it is possible to enable a setting in Google Chrome which allows requests to the local file system from the browser, but it's really not a good idea to use it. If you decide you want to go ahead and do this anyway, a guide can be found here.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • it can be done, but will have to change the setting of chrome first, which is definitely not advisable but can be done – Deep Jul 13 '16 at 07:00
  • @Deep true, but that's not a good idea as it leaves your machine very vulnerable. – Rory McCrossan Jul 13 '16 at 07:00
  • totally agree with you on this. – Deep Jul 13 '16 at 07:01
  • I added a note about it if the OP wants to, but it's entirely caveat emptor – Rory McCrossan Jul 13 '16 at 07:02
  • @Deep : there have no way without creating web server ? – vibog Jul 13 '16 at 07:07
  • @RoryMcCrossan : Thank you sir for giving quick answer :) – vibog Jul 13 '16 at 07:07
  • @vibog no problem, glad to help – Rory McCrossan Jul 13 '16 at 07:08
  • @RoryMcCrossan : But sir , Can I do that without webserver – vibog Jul 13 '16 at 07:09
  • if you want to by pass the chrome security than there is – Deep Jul 13 '16 at 07:09
  • You can use the setting I mentioned in Chrome, but as I stated, I would strongly suggest you use a webserver. If you're on windows it's literally a 2 minute job to install one. – Rory McCrossan Jul 13 '16 at 07:10
  • @vibog , some else also wanted to do this for testing purpose , this is the answer i provided, here you go http://stackoverflow.com/questions/38288446/reading-local-xml-from-javascript/38289284#38289284 – Deep Jul 13 '16 at 07:12
  • @RoryMcCrossan : Yes sir , You right I have no problem , your solution is wonderful . Actually I am working on microhttpd c++ server which is created by me . But that above jquery code just I made a simple , If now I integrate it with microhttpd server , will that work ? I don't now . That I want know microhttpd support that type request . – vibog Jul 13 '16 at 07:15
  • @RoryMcCrossan _"as it leaves your machine very vulnerable"_ Can you describe how leaves machine vulnerable? – guest271314 Jul 13 '16 at 07:20
  • @guest271314 Because it means I can run JS code which can access any file on your local machine. – Rory McCrossan Jul 13 '16 at 07:22
  • @RoryMcCrossan _"which can access any file on your local machine."_ How could you run code on another users machine when user is at `file:` protocol which could access any file at users local machine? – guest271314 Jul 13 '16 at 07:24
  • 1
    The security setting is update on/in the Chrome browser itself! (Global setting) So doesnt matter if the user is trying to load a local file.. if they go a webstie, that has some js code in it.. it can access files in your local file system. – whispers May 25 '17 at 22:17