ajax前后台交互
////////////////单个参数///////////////////
前台:
$("#operatecom").click(function(){
var id = $("#operatestate").val();
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/testManage/testup",
async: false,
contentType: "application/json; charset=utf-8",
data:id,
dataType: "text",
success: function (message) {
alert(message)
},
error: function (message) {
}
});
});
------------------------------------------------------
后台:
@RequestMapping("/testManage/testup")
public void auditExtractup(@RequestBody String id,HttpServletResponse response) {
String message = "";
if ("1".equals(id)) {
message = "操作成功";
} else {
message = "操作失败";
}
PrintWriter out = null;
try {
out = response.getWriter();
out.print(message);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
out.close();
}
}
}
////////////////多个参数///////////////////
前台:
function saveInfo(){
var name = $("#name").val();
var age = $("#age").val();
var address = $("#address").val();
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/student/saveInfo",
async: false,
//contentType: "application/json; charset=utf-8", -----------------注释掉这句话,否则控制层中获取不到参数值
data:{name:name,age:age,address:address},
dataType: "text",
success: function (message) {
alert(message)
},
error: function (message) {
}
});
}
------------------------------------------------------
后台:
@RequestMapping("/student/saveInfo")
public String saveInfo(HttpServletRequest request, Model model){
String name = request.getParameter("name");
String age = request.getParameter("age");
return name;
}