[AJAX] JS, should read an XML, but don't happen

M

Mariano

I've copied this frame of code from book Ajax for dummies, but i'm not
able to make it working. The source is copied, and i'm not so good
with debugging of js since i've started few days ago with it.

This is the guests.html file:
_______________________________________________________________________
<html>
<head>
<title>Using Ajax and XML</title>
<script language = "javascript">
function getGuest() {
var mozillaFlag = false;
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
XMLHttpRequestObject.overrideMimeType("text/xml");
mozillaFlag = true;
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
ActiveXObject("Microsoft.XMLHTTP");
}
if (XMLHttpRequestObject) {
XMLHttpRequestObject.open("GET", "guests.xml", true);
XMLHttpRequestObject.onreadystatechange = function() {
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;
if(mozillaFlag) {
removeWhitespace(xmlDocument);
}

displayGuest(xmlDocument);
}
}

XMLHttpRequestObject.send(null);
}
}

function displayGuest (xmldoc) {
firstnamenodes = xmldoc.getElementsByTagName("first_name");
lastnamenodes = xmldoc.getElementsByTagName("last_name");
var displayText = "The main guest was: " +
firstnamenodes[2].firstChild.nodeValue + ' '
+ lastnamenodes[2].firstChild.nodeValue;
var target = document.getElementById("targetDiv");
target.innerHTML=displayText;
}

function removeWhitespace(xml) {
var loopIndex;
for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++)
{
var currentNode = xml.childNodes[loopIndex];
if (currentNode.nodeType == 1) {
removeWhitespace(currentNode);
}
if (((/^\s+$/.test(currentNode.nodeValue))) &&
(currentNode.nodeType == 3)) {
xml.removeChild(xml.childNodes[loopIndex--]);
}
}
}
</script>
</head>
<body>
<h1>Using Ajax and XML</h1>
<form>
<input type=submit onclick=getGuest()>
</form>
<div id="targetDiv" width =100 height=100>
Who was the main guest?
</div>
</body>
</html>
_______________________________________________________________________

And this is the guests.xml file:
_______________________________________________________________________
<?xml version="1.0"?>
<events>
<event type="informal">
<event_title>15th award ceremony</event_title>
<event_number>1207</event_number>
<subject>gala event</subject>
<date>7/4/2006</date>
<people>
<person attendance="present">
<first_name>Sam</first_name>
<last_name>Edwards</last_name>
</person>
<person attendance="absent">
<first_name>Sally</first_name>
<last_name>Jackson</last_name>
</person>
<person attendance="present">
<first_name>Cary</first_name>
<last_name>Grant</last_name>
</person>
</people>
</event>
</events>
_______________________________________________________________________

Thus, when the button is clicked nothing happen as you can see in this
page:
http://mariano.altervista.org/ajax/cartella/guests.html
Indeed what should be happen at button click is the replacing of the
string:
Who was the main guest?
with the string:
The main guest was Gary Chant.

Every help solution will be appreciate.
 
P

pr

Randy said:
[...] The problem is your quotation
marks. In the page itself, they are “ and ”, they should be " or '.
Change ALL of them to single quotes ' or double quotes ". I don't even
know how to type those two characters on my keyboard that you used, I
had to copy/paste them.

Use a word processor with 'smart quotes' enabled, which I suspect is
what happened here.
 
M

Mariano

Mariano said the following on 11/22/2007 6:34 AM:



<snip>

Don't indent code with tabs to post to Usenet.


The quotes didn't come through properly (or I don't see the same quote
marks here I see in the actual page). The problem is your quotation
marks. In the page itself, they are " and ", they should be " or '.
Change ALL of them to single quotes ' or double quotes ". I don't even
know how to type those two characters on my keyboard that you used, I
had to copy/paste them.

Another problem is that IE7, with native XMLHTTP Support enabled, will
throw an error on the overrideMimeType line as it doesn't support it.



Correct the quotation marks, test it again, try to figure out the
errors, and post back with a new updated test page if you can't figure
it out.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Thank you both and compliment for the attention to the type of
quotes ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

AJAX and XPATH 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top