Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX request to local file system not working in Chrome? [duplicate]

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>");         });     } }); 
like image 549
vibog Avatar asked Jul 13 '16 06:07

vibog


People also ask

How do I send AJAX requests every second?

ajax({ type: 'POST', url: 'increment. php', data: $(this). serialize(), dataType: 'json', success: function (data) { $('#hidden'). val(data);// first set the value }, complete: function (data) { // Schedule the next setTimeout(doAjax, interval); } }); } setTimeout(doAjax, interval);

How do I fix cross origin requests are only supported for protocol schemes Chrome Chrome extension https?

Just change the url to http://localhost instead of localhost . If you open the html file from local, you should create a local server to serve that html file, the simplest way is using Web Server for Chrome . That will fix the issue.

Why is AJAX success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

Does AJAX work with local files?

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.


1 Answers

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.

like image 128
Rory McCrossan Avatar answered Oct 14 '22 01:10

Rory McCrossan