ajax响应中文乱码问题及解决方法
源码及现象
使用ajax函数调用controller后返回的json字符串被页面接收后中文显示为问号。
设置了tomcat,jsp页面,controller,web.xml,ajax等的编码格式都不管用。
后发现浏览器接收返回的数据的格式为ISO-8859-1,怎么也设置不了其他的格式。
现象图:
其源码如下
Jsp+ajax
function ajaxwork(user_account,password,validCode){
$.ajax({
url:"check",
type:"get",
contentType: "application/json;charset=UTF-8",
dataType:'text',
data:{
"user_account":user_account,
"password":password,
"validCode":validCode
},
beforeSend:function(){
},
success:function(data){
var dataObj = eval("("+data+")");
if(dataObj.success){
alert("登陆成功");
window.location.href="${pageContext.request.contextPath}/index.jsp";
}else if(dataObj.element=="validCode"){
addClass("vcode_A","vcode_B", dataObj.message);
$("#div3").addClass("has-error redLight"); ;
}else {
addClass("pwd_A","pwd_B", dataObj.message);
}
changeCode();
}
});
Controller
@RequestMapping(value="/check")
@ResponseBody
public Json loginByTaxNum(
@RequestParam(value="user_account", required=true) String user_account,
@RequestParam(value="password", required=true) String password,
@RequestParam(value="validCode", required=true) String validCode,
HttpSession session,
HttpServletResponse response,
HttpServletRequest request) throws UnsupportedEncodingException{
//获取验证图片的验证码
Json json = new Json();
String kaptcha = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
if(kaptcha.equalsIgnoreCase(validCode)){
//对比验证图片的验证码和用户输入的验证码
User user = null;
try {
user = userService.getUserbyName(user_account);
if (user==null) {
return “{message:‘账户不存在’}”;
}
String password2 = user.getPassWord();
if(!password.equals(password2)){
return “{message:‘密码错误’}”; }
} catch (Exception e) {
e.printStackTrace();
}
session.setAttribute("user", user);
json.setSuccess(true);
return “{success:‘账户不存在’}”;
}
json.setElement("validCode");
json.setMessage("验证码错误");
return “{element:‘validCode’,message:‘验证码错误’}”;
}
}
解决方法
1,原因@responseBody注解自动将对象转换成json字符串,所以只返回对象就行,不用返回字符串(至于原因还不太清楚,求知道的大神指点)。
2,解决方法,将需要返回的信息封装到对象中(这里本人设置了一个Json类,将数据封装到里面),修改代码如下(jsp的代码不变,只修改controller的代码)
@RequestMapping(value="/check")
@ResponseBody
public Json loginByTaxNum(
@RequestParam(value="user_account", required=true) String user_account,
@RequestParam(value="password", required=true) String password,
@RequestParam(value="validCode", required=true) String validCode,
HttpSession session,
HttpServletResponse response,
HttpServletRequest request) throws UnsupportedEncodingException{
//获取验证图片的验证码
Json json = new Json();
String kaptcha = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
if(kaptcha.equalsIgnoreCase(validCode)){
//对比验证图片的验证码和用户输入的验证码
User user = null;
try {
user = userService.getUserbyName(user_account);
if (user==null) {
json.setMessage("账户不存在");
return json;
}
String password2 = user.getPassWord();
if(!password.equals(password2)){
json.setMessage("密码错误");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
session.setAttribute("user", user);
json.setSuccess(true);
return json;
}
json.setElement("validCode");
json.setMessage("验证码错误");
return json;
}
}
修改后页面显示正常