【JS】中ajax的URL中包含中文,后台接收乱码
【问题】
ajax提交get请求,url中参数包含中文,后台接收到显示乱码。
【解决方案】
前台:
function getSiteInfoByName(siteName){ var res; $.ajax({ url : getRootPath() + "/site/getSiteInfoByName.do?" + "siteName=" + encodeURI(encodeURI(siteName)), type : "get", contentType : "application/json; charset=utf-8", dataType : "json", async : false, success : function(data) { res = data; }, error : function(data) { alert("获取局点信息失败!"); res = null; } }); return res; }
后台:
@RequestMapping(value = "/getSiteInfoByName.do") @ResponseBody public VSiteWithBLOBs getSiteInfoByName(@RequestParam("siteName") String siteName) { logger.info("Get site int by name : {}.",siteName); String site_name=""; try { site_name = URLDecoder.decode(siteName, "UTF-8"); } catch (UnsupportedEncodingException e) { logger.error("decode chinese character failed :",e); } logger.info("Get site int by name : {}.",site_name); return siteService.getSiteByName(site_name); }