ajax xmlhttp响应为null - javascript

时间:2013-02-12 20:04:30

标签: javascript ajax null response

这是我的代码:

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

我得到:不能为此行调用null的方法'getElementsByTagName':

var response = xmlhttp.responseXML.getElementsByTagName(“theComment”)[0] .text;

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

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

            e.printStackTrace();
        }

浏览器是chrome。谁能告诉我为什么xmlhttp.responseXML是nul ??

1 个答案:

答案 0 :(得分:0)

<br>在xml try <theComment>asasasaasa<br/></theComment>中无效。