Load local xml file using javascript in Google Chrome

Programming for Search Engines 101. An area for avid PHP and .NET developers to chat about Programming techniques and how to make better use of search engines.

Moderator: Moderators

Load local xml file using javascript in Google Chrome

Postby sreenavc » Fri Aug 19, 2011 3:49 am

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.
Attachments
space-calculation.rar
(46.76 KiB) Downloaded 633 times
sreenavc
 
Posts: 21
Joined: Fri Feb 18, 2011 3:59 am
Tell us why you would like to become a WyseLabs Member:

Re: Load local xml file using javascript in Google Chrome

Postby douglas » Fri Aug 19, 2011 4:43 am

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.
douglas
 
Posts: 282
Joined: Fri Feb 20, 2009 3:56 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests