ajax xmlhttp response is null - javascript

ajax xmlhttp response is null - javascript

本文关键字:javascript null is xmlhttp response ajax      更新时间:2023-09-26

这是我的代码:

<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>

I get:无法为此行调用null的方法"getElementsByTagName":

var response=xmlhttp.responseXML.getElementsByTagName("注释")[0].text;

servlet发送了响应,因为在调试器中它表示已收到repsonse,但无论如何,servlet内部的代码是:

response.setContentType("text/xml");
        try {
            response.getWriter().println("<theComment>asasasaasa<br></theComment>");
        } catch (IOException e) {
            e.printStackTrace();
        }

浏览器是镀铬的。有人能告诉我为什么xmlhttp.responseXML是空的吗??

<br>在xml中无效,请改用<theComment>asasasaasa<br/></theComment>