html+js+servlet 前端接收不到结果

html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Js</title>  
</head>  

<body>  
    <input type="text" id="param1" />  
    <input type="text" id="param2" />  
    <button onclick="ajax()">Go!</button>  
</body>  
</html>  
<script>  
    function createXMLHttpRequest() {  
        var XMLHttpRequest1;  
        if (window.XMLHttpRequest) {  
            XMLHttpRequest1 = new XMLHttpRequest();  
        } else if (window.ActiveXObject) {  
            try {  
                XMLHttpRequest1 = new ActiveXObject("Msxml2.XMLHTTP");  
            } catch (e) {  
                XMLHttpRequest1 = new ActiveXObject("Microsoft.XMLHTTP");  
            }  
        }  
        return XMLHttpRequest1;  
    }  
    function ajax() {  
        //param1与param2就是用户在输入框的两个参数  
        var param1=document.getElementById("param1").value;  
        var param2=document.getElementById("param2").value;  
        var XMLHttpRequest1 = createXMLHttpRequest();  
        //指明相应页面    
        var url = "http://192.168.0.111:8080/AjaxServer/Test";  
        XMLHttpRequest1.open("POST", url, true);  
        //这里没法解释,你所有JavaScript的请求头都这样写就对了,不会乱码    
        XMLHttpRequest1.setRequestHeader("Content-Type",  
                "application/x-www-form-urlencoded");  
        //对于ajaxRequest,本js.html将会传递param1与param2给你。    
        XMLHttpRequest1.send("param1=" + param1 + "&param2=" + param2);  
        //对于返回结果怎么处理的问题    
        XMLHttpRequest1.onreadystatechange = function() {  
            //这个4代表已经发送完毕之后    
            if (XMLHttpRequest1.readyState == 4) {  
                //200代表正确收到了返回结果    
                if (XMLHttpRequest1.status == 0) { 
                    //弹出返回结果    
                    alert(XMLHttpRequest1.responseText);  
                } else {  
                    //如果不能正常接受结果,你肯定是断网,或者我的服务器关掉了。    
                    alert("网络连接中断!");  
                }  
            }  
        };  
    }  
</script> 

servlet:

 /**
 * Servlet implementation class Test
 */
@WebServlet("/Test")
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html; charset=utf-8");  
        PrintWriter pw = response.getWriter();  
        request.setCharacterEncoding("utf-8");  
        String param1=request.getParameter("param1");  
        System.out.println("param1="+param1);
        String param2=request.getParameter("param2");    
        System.out.println(",param2="+param2);
        pw.print("param1="+param1+",param2="+param2);  
        pw.flush();  
        pw.close();
    }
}

前段发送的请求servlet收到了,并且处理了,可是前段接收到结果总是为空,并且XMLHttpRequest1.status==0,求问哪里出了问题

1个回答

后台控制台这些

  String param1=request.getParameter("param1");  
        System.out.println("param1="+param1);

有输出吗

dyjscq
dyjscq 回复心似一片青苔: Request.status==0,不等于200
3 年多之前 回复
hope_It
i大俊 回复dyjscq: XMLHttpRequest1.status==0,改为XMLHttpRequest1.status==200,就能请求成功并输出
3 年多之前 回复
dyjscq
dyjscq 有输出
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问