1,頁面Ajax:主要是代碼加紅ajax
var data = {userName:$("#userName").val(), password:$("#password").val()}
$.ajax({
type: "post",
url: "/login/login",
data: JSON.stringify(data),
contentType: "application/json",
success: function(data){
}
});json
後臺代碼:app
@RequestMapping("login")
@ResponseBody
public ResultInfo login(@RequestBody JSONObject json) {post
System.out.println(json);url
}spa
----------------------------------------------------------對象
JSONObject 也能夠改爲封裝的對象如:User對象string
---------------------------------------------------------io
2,使用@ModelAttribute 註解function
頁面Ajax代碼:
var data = {userName:$("#userName").val(), password:$("#password").val()}
$.ajax({
type: "post",
url: "/login/login",
data: data,
success: function(data){
window.location.href="http://localhost:8899/index/index";
}
});
後臺代碼:
@RequestMapping("login")
@ResponseBody
public ResultInfo login(@ModelAttribute("UserVo") UserVo param) {
System.out.println(json);
}
使用@ModelAttribute註解時,不能使用JSONObject。