这是我的代码:
<script type="text/javascript">
var xmlhttp;
$(document).ready(function (){
xmlhttp=new XMLHttpRequest();
});
function SubmitCommentAJAX(i){alert();
alert(i.parentNode.getElementsByClassName("commentsScroll")[0].innerHTML);
var thecomment=i.parentNode.getElementsByClassName("styled")[0].innerHTML;
var commentBox=i.parentNode.getElementsByClassName("commentsScroll")[0];
var request="http://localhost:8080/ituned.com/index?Event=Comment&PostTitle=<%=p.getTitle()%>&PostOwner=<%=p.getUsername_of_Owner()%>&comment="+thecomment;
xmlhttp.open("POST",request,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response=xmlhttp.responseXML.getElementsByTagName("theComment")[0].text;
commentBox.insertBefore(response, commentBox.firstChild);
}
};
}
</script>
我得到:Cannot call method 'getElementsByTagName' of null for this line:
var response=xmlhttp.responseXML.getElementsByTagName("theComment")[0].text;
servlet 发送了响应,因为在调试器中它说收到了响应,但无论如何这里是 servlet 中的代码:
response.setContentType("text/xml");
try {
response.getWriter().println("<theComment>asasasaasa<br></theComment>");
} catch (IOException e) {
e.printStackTrace();
}
浏览器是chrome。谁能告诉我为什么 xmlhttp.responseXML 是空的?