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>"); }); } });
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);
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With