AJAX documentElement为null

时间:2014-11-03 19:49:48

标签: javascript php ajax document

我收到错误:未捕获TypeError:无法读取属性' documentElement'为null 使用此代码:

function respondHandler() {
if(xmlHttp.readyState == 4){
  if(xmlHttp.status == 200){
    xmlResponse = xmlHttp.responseXML;
    xmlDocumentElement = xmlResponse.documentElement;
    message = xmlDocumentElement.firstChild.data;
    document.getElementById('output').innerHTML = message;

}else{
alert('something went really wrong');
}

}

}

它首先奏效了,现在它已经开始了。我真的不知道出了什么问题!

1 个答案:

答案 0 :(得分:0)

试试这个

function respondHandler() {
  // get element reference here
  var element = document.getElementById('output');
  if(xmlHttp.readyState == 4){
    if(xmlHttp.status == 200){
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
      message = xmlDocumentElement.firstChild.data;
      element.innerHTML = message;

  }else{
    alert('something went really wrong');
  }
}