/    Sign up×
Community /Pin to ProfileBookmark

Ajax XML response, errors when empty

Basically, it links to a PHP file which gets information from a database and formats it as XML. The problem is, I get javascript errors when I try to assign a tag value to a string when the xml tag is empty.

The error happens about here

[CODE]var title = xmlDocument.getElementsByTagName(“title”)[0].firstChild.data;
var description = xmlDocument.getElementsByTagName(“keywords”)[0].firstChild.data;
var keywords = xmlDocument.getElementsByTagName(“description”)[0].firstChild.data;
var content = xmlDocument.getElementsByTagName(“content”)[0].firstChild.data;[/CODE]

I think the error is something along the lines of ‘Object expected’;

heres the code…

[CODE]<script language=”javascript”>
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject(“Microsoft.XMLHTTP”);
}

function getData(dataSource, divID) {
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open(“POST”, dataSource);
XMLHttpRequestObject.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);
XMLHttpRequestObject.onreadystatechange = function() {
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
xmlDocument = XMLHttpRequestObject.responseXML;
var title = xmlDocument.getElementsByTagName(“title”)[0].firstChild.data;
var description = xmlDocument.getElementsByTagName(“keywords”)[0].firstChild.data;
var keywords = xmlDocument.getElementsByTagName(“description”)[0].firstChild.data;
var content = xmlDocument.getElementsByTagName(“content”)[0].firstChild.data;
document.write(title);
document.write(description);
document.write(keywords);
document.write(content);
}
}
XMLHttpRequestObject.send(“id=25”);
}
}
</script>[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterAug 07.2007 — xmlDocument.getElementsByTagName("keywords")[0].firstChild.data will generate an error if it selects [color=blue]<keywords />[/color] since is contains no children for firstChild to reference hence firstChild.data generates an error.

To isolate the error from your Ajax, this is essentially what you are doing:
<i>
</i>&lt;script type="text/javascript"&gt;
k=document.createElement("keywords");
alert(k.firstChild)
alert(k.firstChild.data)//error
&lt;/script&gt;

the way to correct it would be:
<i>
</i>&lt;script type="text/javascript"&gt;
k=document.createElement("keywords");

function checkIt(){
if(k.firstChild)alert("&lt;"+k.nodeName+"&gt;"+k.firstChild.data+"&lt;/"+k.nodeName+"&gt;");
else alert("no children");
}

checkIt()
k.appendChild(document.createTextNode("ntextn"));
checkIt()
&lt;/script&gt;
×

Success!

Help @littlened spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...