ajax得到后端数据一直提示为[object Object]解决方法

时间:2021-10-23 22:41:37

前段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不出来

ajax得到后端数据一直提示为[object Object]解决方法

百度了好久,使用  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>

结果:

ajax得到后端数据一直提示为[object Object]解决方法