SpringMVC使用json傳輸資料的問題
阿新 • • 發佈:2019-02-05
1.在SpringMVC框架上使用json傳輸資料出現了後臺中文資料傳到前臺變成"???"的問題,解決方法如下:
@ResponseBody @RequestMapping(value = "/accountManagement.getMessage",method = RequestMethod.POST, produces = "text/json;charset=UTF-8") public String doGetMessage(){ Map map = getUserService().getUserMessage(Param.getUserid()); JSONObject json = new JSONObject(); json.put("userid",Param.getUserid()); json.put("username",map.get("username")); json.put("gender",map.get("gender")); json.put("education",map.get("education")); return json.toString(); }
新增produces = "text/json;charset=UTF-8"就能解決問題。
2.前臺向後臺傳輸陣列資料的時候,程式碼應為
$.ajax({
url: "",
type: "post",
data: {
array: new Array(),
},
dataType: "json",
success: function(data){},
})
後臺使用@RequestParam(value ="array[]")String[] array
來接收。