AJAX发送请求,后台接受到数据但是返回结果后,Ajax请求结果失败

前台JS和后台相应如下:

 var username = $("#user_name").val().trim();
    var password = $("#password").val();
    console.log(username+","+password);
    $.ajax({
        url:"/Test1224/login.action",
        data:{"username":username,"password":password},
        dataType:"json",
        type:"POST",
        success:function(result){
            console.log("success");
            console.log(result);
        }
    });
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
        System.out.println("enter doPost");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println(req.getParameter("username")+","+req.getParameter("password"));
        PrintWriter pw = resp.getWriter();
        Result result = new Result();
        result.setFlag(true);
        result.setName(req.getParameter("username"));
        pw.print(result);
        pw.close();
    }

错误提示:POST http://127.0.0.1:9080/Test1224/login.action 405 (Method Not Allowed)

/Test1224/login.action这个地址不允许post请求,检查下你的配置

换个action名试试 我也发生过这样奇葩的问题 明明后台处理了却没有返回换个action名就好了

你print后立马close会不会有影响。

问题已解决,将super.doPost(req, resp);删除掉就好了。进入super.dopost()一下就明白了