jquery ajax servlet取值
请教,如何从servlet中把jQuery的ajax中以json格式传入的数据接收到?javaScript:$('#but_json_json').click(funct...
请教,如何从servlet中把jQuery的ajax中以json格式传入的数据接收到?
javaScript:
$('#but_json_json').click(function(){
var j ={"name":"王","password":123456};
$.ajax(
{
url:"servlet/JsonObject", //访问路径
type:"POST", //访问方式
data:j, //传入服务端的数据
dataType:"json",
contentType:"application/json;charset=utf-8",
success : function(data){
alert(data);
alert(data.name);
alert(data.password);
}
}
);
});
Servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//如接收客户端传入的对象j?
//此处接收到的都是null
String user = request.getParameter("j");
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(user);
//JSON对象
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("password", password).accumulate("name", "www");
response.setContentType("application/json");
response.getWriter().write(jsonObject.toString());
}
希望知道的朋友指点一二 展开
javaScript:
$('#but_json_json').click(function(){
var j ={"name":"王","password":123456};
$.ajax(
{
url:"servlet/JsonObject", //访问路径
type:"POST", //访问方式
data:j, //传入服务端的数据
dataType:"json",
contentType:"application/json;charset=utf-8",
success : function(data){
alert(data);
alert(data.name);
alert(data.password);
}
}
);
});
Servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//如接收客户端传入的对象j?
//此处接收到的都是null
String user = request.getParameter("j");
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(user);
//JSON对象
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("password", password).accumulate("name", "www");
response.setContentType("application/json");
response.getWriter().write(jsonObject.toString());
}
希望知道的朋友指点一二 展开
展开全部
String param1 = request.getParameter("param1");
json 改为 var p={}; p.param1:"abcd";或者 p={param1:"abcd",param2:"def"};试试
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { init(request,response);}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { init(request,response);}
public void init(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { 主程序内容}
json 改为 var p={}; p.param1:"abcd";或者 p={param1:"abcd",param2:"def"};试试
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { init(request,response);}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { init(request,response);}
public void init(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { 主程序内容}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
data:{"name":"王","password":123456}
之后后台 String name= request.getParameter("name");
String password = request.getParameter("password");
之后后台 String name= request.getParameter("name");
String password = request.getParameter("password");
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询