nmuntz
nmuntz

Reputation: 1158

Ajax Parsing Local XML

I'm building a local html file to show dynamically some data from an XML file and by using Chrome's Inspector I figured my XML file is not being parsed because it is "not hosted on a webserver"

XMLHttpRequest cannot load data.xml. Cross origin requests are only supported for HTTP.

I know that there are a few flags I could pass to Chrome/web browser to workaround this limitation but I'm looking into some alternative method. I will probably have to distribute this files to a few people and teaching them how to pass flags to the browser is not an option for me. Hosting them on a web server is not an option either.

Thanks a lot in advance.

Upvotes: 0

Views: 1242

Answers (5)

Dr.Molle
Dr.Molle

Reputation: 117364

I'm afraid if chrome only provides options that you don't like to apply, your application and chrome will not come together. Access via iframe & object does'nt work too, load()-method of createDocument is not supported by chrome(if it does I guess you got the same error).
Whatever you try will be a sideway to pass chrome's restrictions, what cannot be good, because I think they have good reasons to setup these restrictions.

Upvotes: 0

goat
goat

Reputation: 31854

Unless there's a compelling reason to have a separate html and xml file, you could just put the data directly in the html file.

Upvotes: 0

zzzzBov
zzzzBov

Reputation: 179284

If you're willing to use a library you can use jQuery's AJAX method to perform a cross-domain request (i'm not entirely certain that jQuery will support what you're trying to do). JSONP works, but you have XML data...

You could try loading it in a script tag anyway and see if you can get the innerHTML value without breaking the script; once you're done getting the text from it, remove the script from the page. You may be able to get at the data before the browser tries to parse the script by attaching onload and onreadystatechange events to the script element.

var s = document.createElement('script');
s.src = '/path/to/file.xml';
s.onload = s.onreadystatechange = getData;
function getData(e)
{
  //get the text out of the script element
  //remove the event handler from the script element
  //remove the script from the page
}
document.getElementsByTagName('body')[0].appendChild(s);

I didn't test it, but it might work.

Upvotes: 1

arifwn
arifwn

Reputation: 131

How about setting up a local webserver? XAMPP should be easy to install even for novice. Just tell them to put your files in the htdocs folder, run xampp and start the apache server.

Upvotes: 0

Anthony Corbelli
Anthony Corbelli

Reputation: 877

No ghost unless you set up a local server or host the XML file locally. Ajax has to follow the same origin policy.

Upvotes: 1

Related Questions