前台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)
問題已解決,將super.doPost(req, resp);刪除掉就好了。進入super.dopost()一下就明白了