Page 1 of 1

Load local xml file using javascript in Google Chrome

PostPosted: Fri Aug 19, 2011 3:49 am
by sreenavc
When we open the webpage that contains the javascript code to load xml file locally in chrome , get the following error
"XMLHttpRequest cannot load file:///C:/Temp/category.xml. Cross origin requests are only supported for HTTP."

Local file access is disabled by default for security reasons. It would be more secure if you just start a local webserver and fetch your html and xml from localhost.If you're using Chrome, and you have a local HTML file that uses Javascript to load an XML file , it just won't work.

To get around this use a script tag ,not XMLHttpRequest. Here's an example with JSON.

First create json data equivalent to the xml file and write into a local file called say category.js

Then get the file from your local html page using:-

<script type="text/javascript" src="category.js"></script>

So you need not use a .json file and XMLHttpRequest,just a plain old script tag.

Re: Load local xml file using javascript in Google Chrome

PostPosted: Fri Aug 19, 2011 4:43 am
by douglas
Good Post Sreena. A very peculiar type of chrome browser issue and specific solution.

But I don't think we need to go for this solution normally because this will be automatically resolved when we upload the files to any of the server/loaclhost because the XMLHttpRequest has limitation to load file only from local machine and not from any localhost or server.

These piece of information are really worth so keep post on these type of technical issues and solution.