csdnceshi62 2016-07-16 06:38 采纳率: 100%
浏览 24

AJAX脚本不起作用

I cannot get the origin IP address from the httpRequest object using the following JavaScript. xhttp.responseText return a null value. I'd appreciate your help.

 <script type="text/javascript" language="JavaScript">
       var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
           if (xhttp.readyState == 4 && xhttp.status == 0) {
              document.getElementById("LOCAL_IP").value = xhttp.responseText;
           }
       };
       xhttp.open("GET", "http://11.5.2.218:4080/getIP.jsp", true);
       xhttp.send();
  </script>

the getIP.jsp file content is

Your IP is <%=request.getRemoteAddr()%>
  • 写回答

1条回答 默认 最新

  • weixin_33690963 2016-07-16 07:12
    关注

    Hey,

    You need the xhttp.status to be equal to 200, not 0.

    For more information about server status codes, read this HTTP status codes tutorial

    Try this:

    xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        document.getElementById("LOCAL_IP").value = xhttp.responseText;
      }
    };

    </div>
    
    评论

报告相同问题?