9
回答
servlet获取不到Jquery ajax post过来的数据
【腾讯云】学生服务器套餐10元/月 >>>   
前台jspjsp页面有如下代码:
var par={a:"dasdadasdadasd",b:"哈哈"};
var options = {
                     url: "servlet/Usrservlet",
                     type: "POST",                    
                     dataType: "json",     
                     contentType: "application/json; charset=utf-8",                
                     data:par ,
                     async:false,
                    success: function (data) {
                         alert(data.detail);
                     }
                 };
 
                $.ajax(options);
后台servlet:
String name=request.getParameter("a");
String address=request.getParameter("b");
取到的值为空,但是将type设置为GET方式时,可以获取到值,这是为什么?不换成get方式,怎样才能获取到值?
举报
工大小生
发帖于4年前 9回/8K+阅

以下是问题补充:

  • @工大小生 :问题以解决,去掉contentType就ok,原因不明,有朋友说抓http包,只因我是新手,没看出什么端倪,谢谢大家! (4年前)
共有9个答案 最后回答: 4年前

贴一下你servlet全部的代码,你在哪个方法里面取值的呢,servlet分post和get两个方法的

--- 共有 3 条评论 ---
面向阳光回复 @工大小生 : 不注释的话返回的是json对象,不是单个变量形式,解析json对象就能拿到值了 4年前 回复
小小志回复 @工大小生 : application/json意思是发json格式的数据 4年前 回复
工大小生 //contentType: "application/json; charset=utf-8", 注释该句代码之后可以获取到了,不知道为什么啊 4年前 回复

引用来自“魔力猫”的评论

贴一下你servlet全部的代码,你在哪个方法里面取值的呢,servlet分post和get两个方法的

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        String name=request.getParameter("a");
        String address=request.getParameter("b");
        response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String returnMsg = "{\"flag\":\"success\",\"detail\":\"恭喜您!登录成功\"}";
System.out.println(name);
System.out.println(address);
out.write(returnMsg);
out.flush();
out.close();

}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("a");
        String address=request.getParameter("b");
        response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String returnMsg = "{\"flag\":\"success\",\"detail\":\"恭喜您!登录成功\"}";
System.out.println(name);
System.out.println(address);
out.write(returnMsg);
out.flush();
out.close();
        
}
public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String name=request.getParameter("a");
        String address=request.getParameter("b");
        response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String returnMsg = "{\"flag\":\"success\",\"detail\":\"恭喜您!登录成功\"}";
System.out.println(name);
System.out.println(address);
out.write(returnMsg);
out.flush();
out.close();
}

url: "servlet/Usrservlet", url 不对,要加入 ,项目 如 你的 项目是  127.0.0.1:8080/test

那么 url:url: "/test/servlet/Usrservlet", 

--- 共有 1 条评论 ---
工大小生已经执行了doPost函数,说明不是url的问题 4年前 回复
顶部