- Ajax向服务器提交数据
可以采用$.ajax,$.get或者$.post,这里采用$.post为例子
$.post("jsontest.action",{v1:"12345",v2:"12345"},function(result){
$("p#1").html(result.user.name);
$("p#2").html(result.user.age);
},"json");
- Struts获取Ajax提交的数据并给出响应
// 获取request对象
HttpServletRequest req = ServletActionContext.getRequest();
// 获取数据
String v1 = req.getParameter("v1");
String v2 = req.getParameter("v2");
// 获取response对象
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();
// 响应
User user = new User();
user.setAge(15);
user.setName("l11eo");
JSONObject obj = new JSONObject();
obj.accumulate("user",user);
out.print(obj.toString());
out.flush();
out.close();
returnNONE;
原文链接:https://www.f2er.com/ajax/166554.html