开发者

has no method 'getElementsByTagName' error makes no sense

开发者 https://www.devze.com 2023-03-28 04:01 出处:网络
i alert(xmldoc) in it is there? i dont understand here the code: function getChatText() { if (receiveReq.readyState == 4 || receiveReq.readyState == 0)

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开发者_如何学JAVA 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


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.


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.


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');

0

精彩评论

暂无评论...
验证码 换一张
取 消