charlie_cat
charlie_cat

Reputation: 1850

has no method 'getElementsByTagName' error makes no sense

i alert(xmldoc) in it is there? i dont understand here the code:

function getChatText()
{
   if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
   {
      receiveReq.open("GET", 'getChat.php', true);
      receiveReq.onreadystatechange = handleReceiveChat; 
      receiveReq.send(null);
   }
}   

function handleReceiveChat()
{
   if (receiveReq.readyState == 4)
   {
      var chat_div = document.getElementById('div_chat');
      var xmldoc = receiveReq.responseText;
      alert(xmldoc);
      var message_nodes = xmldoc.getElementsByTagName("message"); 
      var n_messages = message_nodes.length
      //more code here
   }
}

part of getChat.php looks like this:

<?php
//code here
  $xml = '<?xml version="1.0" ?>';
  $sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time" . 
    " FROM message WHERE chat_id = " . db_input($_GET['chat']) ;
  $message_query = db_query($sql);
  while($message_array = db_fetch_array($message_query)) 
  {
$xml .= '<message>';
    $xml .= '<message_id id= "' . $message_array['message_id'] . '"</message_id>';
$xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>';
$xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>';
$xml .= '<time>' . $message_array['post_time'] . '</time>';
$xml .= '</message>';
  }
}
echo $xml;
?>

the exact error is on:

var message_nodes = xmldoc.getElementsByTagName("message"); 

and states:

Uncaught TypeError: Object <?xml version="1.0" ?><message><message_id id= "1"</message_id><user>Me, ME, me and mE </user><text>123</text><time>09:14</time></message> has no method 'getElementsByTagName'

can some one please explain this? thank you

Upvotes: 1

Views: 8979

Answers (3)

zixtor
zixtor

Reputation: 241

You haven't sent proper response headers before sending the response in PHP... That's why the responseXML is null.

Try adding in your PHP file:-

header('Content-Type: text/xml');

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817238

xmldoc seems to be a string (receiveReq.responseText), not a DOM element. Strings don't have the getElementsByTagName method.

Try receiveReq.responseXML [spec], this returns a DOM Document object.

See also: XMLHttpRequest [MDN]

Update: I took the XML as it is shown in your post, and ran it through http://validator.w3.org/check.

You XML is not correct, that's why you get null. This is not valid:

<message_id id= "1"</message_id>

Seems you are missing a >. If you are generating that XML manually, then don't. Use a proper XML document builder.

Upvotes: 1

Coomie
Coomie

Reputation: 4868

The responseText (xmldoc) is a string. Strings have methods, but getElementByTagName is not one of them.

getElementByTagName is a method of objects, not strings. eg

var numberOfAnchorTagsInADocument = document.getElementByTagName("a").length;

Please provide more info on what you're trying to do.

Upvotes: 0

Related Questions