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

    有输出吗

    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面