dyjscq 2017-06-06 03:37 采纳率: 0%
浏览 1209

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条回答 默认 最新

  • i大俊 2017-06-06 05:04
    关注

    后台控制台这些

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

    有输出吗

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献