PDA

View Full Version : Using AJAX to load an XML File



hammo
September 7th, 2007, 05:27 AM
I've been going nuts for the past 6 hours trying to get this little script to work, but it simply doesn't!

My goal here is to have it so that when I click box on this page, it will load my WoW character's information into it, but in the .xml format it should be in to begin with (i.e. <characterinfo><name>blah</name> etc).

I've noticed that if I view the site in Safari (I'm a Mac user) it will format the page into HTML, however if I view it in Firefox, it will stay nice and pretty in its XML format.

So, this brings me onto some of my problems.

Can I, simply via AJAX and Safari, have the Armory page come up in XML, and if so, can I have that XML ported to my .html file.

My current code is:
<script type="text/javascript" language="javascript">

function makeRequest(url) {
var httpRequest;

httpRequest = new XMLHttpRequest();
httpRequest.overrideMimeType('text/xml');

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
httpRequest.open('GET', url, true);
httpRequest.send('');

}

function alertContents(httpRequest) {

if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {

window.document.getElementById('out').innerText = httpRequest.responseText;

// var xmldoc = httpRequest.responseXML;
// var root_node = xmldoc.getElementsByTagName('page');
// alert(root_node.firstChild.data);

} else {
alert('There was a problem with the request.');
}
}

}
</script>This works, however it spits out the HTML, which I do not want. Additionally, it gives me the HTML in plain text, so it doesn't actually show up as a formatted page, it shows up as <html><blah><blah>.

I understand I have to use what I've commented off, responseXML, however when I set it to that I am not getting ANYTHING.

For reference, this is the page I want to be loading: http://www.wowarmory.com/character-sheet.xml?r=Shattered%20Hand&n=Syrrage

Any help is much appreciated.

ratherblue
September 7th, 2007, 11:02 AM
=P Only cause you're horde...

Can you show me a link to your page?

You should try something like displaying that data inside a div on your page.


then your javascript would be something like..
document.getElementById("display_div").innerHTML = the_data;

hammo
September 7th, 2007, 01:34 PM
Bah what a simple fix, I was using innerText as opposed to innerHTML :)

However, this doesn't solve the bigger problem, which is the fact that I cannot get these three lines to work:
var xmldoc = httpRequest.responseXML;
var root_node = xmldoc.getElementsByTagName('page');
window.document.getElementById('out').innerHTML = root_node.firstChild.data;

This is the entire page:
<html>
<head>
<style type="text/css">

body {
background: #000;
color: #fff;
}

</style>
<script type="text/javascript" language="javascript">

function makeRequest(url) {
var httpRequest;

httpRequest = new XMLHttpRequest();
httpRequest.overrideMimeType('text/xml');

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
httpRequest.open('GET', url, true);
httpRequest.send('');

}

function alertContents(httpRequest) {

if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {

// window.document.getElementById('out').innerHTML = httpRequest.responseText;

var xmldoc = httpRequest.responseXML;
var root_node = xmldoc.getElementsByTagName('page');
window.document.getElementById('out').innerHTML = root_node.firstChild.data;

} else {
alert('There was a problem with the request.');
}
}

}
</script>

</head>
<body>

<div id="out" onclick="makeRequest('http://wowarmory.com/character-sheet.xml?r=Shattered%20Hand&n=Syrrage')">
Click to see XML
</div>

</body>
</html>

holdaway
September 12th, 2007, 08:36 AM
Did you have any luck getting this to work? Is it easily incorporatable (is that a word?!) into a standard website, php or html? Would you mind if I used the code, once it's working?