最简单的ajax请求

我的ajax请求错哪里了,为什么总是出错

HTML代码

html>
<html>
<head>
  <meta charset="utf-8">
  <title>title>
head>
<body>
<script type="text/javascript">
  var xhr=new XMLHttpRequest()
  window.onload=function(){
    document.getElementById("in").onclick=function(){
      xhr.open("POST","/text1_war_exploded/ajax",true)
      var name=document.getElementById("name").value;
      var pass=document.getElementById("pass").value;
      xhr.setRequestHeader("content-Type","application/x-www-form-urlencoded")
      xhr.send("name="+name+"&pass="+pass)
      xhr.onreadystatechange=function (){
      if(xhr.onreadystatechange==4){
        document.getElementById("span").innerHTML=xhr.responseText}
        if(xhr.onreadystatechange==2){
          document.write("请求已经收到")}
        if(xhr.onreadystatechange==1){
          document.write("服务器已经建立连接")}
      }
    }
  }

script>


用户名<input type="text" name="name" id="name" /><br>
密码<input type="password" name="pass" id="pass" /><br>
<input type="button" name="" id="in" value="提交" />

<span id="span">rfe4span>
body>
html>

Java代码


```java
@WebServlet(value = "/ajax")
public class ajax extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter p=response.getWriter();
        String name=request.getParameter("name");
        String pass=request.getParameter("pass");

       if(name.equals("sb")){
           p.write("用户名不存在或者密码错误");
       }
       else {
           p.write("用户名不存在或者密码错误");
       }




    }
}


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/17721795496614.png "#left")