前端界面
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
<script src="../js/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function () {
})
</script>
</head>
<body>
<div style="margin: 20% 40%">
<h3 id="h3cc">功能</h3>
<input type="button" value="取值触发" id="bt1"/>
<input type="button" value="传Str值触发" id="bt2"/>
<input type="button" value="传Obj值触发" id="bt3"/>
</div>
<script>
$("#bt1").click(function () {
getUser();
});
$("#bt2").click(function () {
paramJson();
});
$("#bt3").click(function () {
bodyJson();
});
function bodyJson(){
var jsonMessage = [{'id':'00005','gender':'男','name':'张小明','type':'用户'},{'id':'00006','gender':'男','name':'李小明','type':'用户'}];
$.ajax({
url : "http://10.0.1.46:8080/springmvc/user/bodyJson",
type : "post",
async : true,
contentType : "application/json",
data : JSON.stringify(jsonMessage),
dataType : 'text',
success : function(data) {
alert(data);
},
error: function () { alert("error"); }
});
}
function paramJson(){
var jsonMessage = {result:"[{'id':'00005','gender':'男','name':'张小明','type':'用户'},{'id':'00006','gender':'男','name':'李小明','type':'用户'}]"};
$.ajax({
url : "http://10.0.1.46:8080/springmvc/user/paramJson",
type : "post",
async : true,
//contentType : "application/json",
data : jsonMessage,
dataType : 'text',
success : function(data) {
alert(data);
},
error: function () { alert("error"); }
});
}
function getUser() {
<!--ajax取值-->
var jsonObject = {
"userId":"admin",
"password":1
};
var jdata={userId:"admin"};
//alert(jdata.userId);
$.ajax({
url : "http://10.0.1.46:8080/springmvc/user/getUser",
type : "post",
async : true,
//contentType : "application/json",
data :{userId:"admin"},
dataType : 'json',
success : function(rs){
alert(rs.type);
},
error: function () { alert("error"); }
});
}
//type:RequestBody
/**/
//type:RequestParam
/*
*/
</script>
</body>
</html>
后端控制层
//接收复杂JSON测试
@RequestMapping(value = "/paramJson")
@ResponseBody
public String paramJson(@RequestParam String result){
/*JSONArray arr = (JSONArray) JSONArray.parse(result);
for(int i=0;i<arr.size();i++){
System.out.println(arr.get(i).toString());
}*/
System.out.println("UserController====paramJson==============================================测试JSON!"+result);
return "success";
}
//接收复杂JSON测试
@RequestMapping(value = "/bodyJson")
@ResponseBody
public String bodyJson(@RequestBody NewUser[] result){
/*JSONArray arr = (JSONArray) JSONArray.parse(result);
for(int i=0;i<arr.size();i++){
System.out.println(arr.get(i).toString());
}*/
System.out.println("UserController====bodyJson==============================================测试JSON!"+result);
return "success";
}
//根据id获得用户信息
@RequestMapping(value = "/getUser")
@ResponseBody
public User getUsers(@RequestParam String userId) {
//调用service方法得到用户列表
User users = userService.selectByPrimaryKey(userId);
System.out.println("UserController==================================================成功查询用户!"+userId);
return users;
}