ajax接收服务端数据中文显示为?的解决办法

ajax响应中文乱码问题及解决方法

源码及现象

使用ajax函数调用controller后返回的json字符串被页面接收后中文显示为问号。

设置了tomcatjsp页面,controllerweb.xmlajax等的编码格式都不管用。

后发现浏览器接收返回的数据的格式为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;

}

}

修改后页面显示正常

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值