1.ResponseBody:
将对象转换为json数据,返回给浏览器。
<a href="${ctp}/ajaxGetAllEmployees">ajax Get All Employees</a>
<script type="text/javascript">
$("a:first").click(function(){
//1.发送ajax请求
$.ajax({
url:"${ctp}/ajaxGetAllEmployees",
type:"GET",
success:function(data){
$.each(data,function(){
var empInfo = this.lastName+"-->"+this.birth+"-->"+this.salary+"-->"+this.email+"-->"+this.gender;
$("#empInfoDiv").append(empInfo+"<br/>");
});
}
});
return false;
});
</script>
@ResponseBody
@RequestMapping("/ajaxGetAllEmployees")
public Collection<Employee> ajaxGetAllEmployees()
{
return employeeDao.getAll();
}
2.RequestBody:
功能1:接收post过来的请求体内容。
功能2:将接收到的json数据(字符串),封装为对象
<a href="${ctp}/testRequestBody">ajax 发送jason数据</a>
<script type="text/javascript">
$("a:first").click(function(){
var emp = {lastName:"Lilith",email:"Lilith@163.com",gender:0};
var str = JSON.stringify(emp);
//1.发送ajax请求
$.ajax({
url : '${ctp}/testRequestBody',
type : "POST",
data : str,
contentType : "application/json",
success:function(data){
/* alert(data); */
}
});
return false;
});
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody Employee employee) {
System.out.println("请求体:"+employee);
return "success";
}