前段ajax
<script type="text/javascript">
function requestJson() {
$.ajax({
type : "post",
url : "checkJosn",
dataType:"json",
success : function(msg) {
alert(msg);
}
});
}
</script后端Controller
@Controller public class JsonController { @ResponseBody @RequestMapping(value = "/checkJosn") public Student checkJosn(){ Student student = new Student(); student.setId(12); student.setUsername("chen"); Address address = new Address(); address.setCity("wuhan"); student.setAddress(address); System.out.println("进入"); return student; } }
提交后,提示object,而注意到浏览器响应里已经得到了json字符串,为啥alert不出来

百度了好久,使用 alert(JSON.stringify(msg));方法可以弹出json字符串。JSON.stringify()方法是将一个JavaScript值(对象或者数组)转换为一个 JSON字符串
修改后ajax代码
<script type="text/javascript">
function requestJson() {
$.ajax({
type : "post",
url : "checkJosn",
dataType:"json",
success : function(msg) {
alert(JSON.stringify(msg));
}
});
}
</script>结果:

转载于:https://www.cnblogs.com/fukua123/p/10568382.html