ajax得到後端數據一直提示為[object Object]解決方法
阿新 • • 發佈:2019-03-21
string alert add req ping ava function check com
前段ajax
<script type="text/javascript"> function requestJson() { $.ajax({ type : "post", url : "checkJosn", dataType:"json", success : function(msg) { alert(msg); } }); } </script
後端Controller
@Controllerpublic 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"> functionrequestJson() { $.ajax({ type : "post", url : "checkJosn", dataType:"json", success : function(msg) { alert(JSON.stringify(msg)); } }); } </script>
結果:
ajax得到後端數據一直提示為[object Object]解決方法