留给自己看
第一种方法将返回类型设置为对象
先创建json对象
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:‘验证码错误’}”;
}
}
第二种:@RequestMapping(value="/getRate",produces={"text/html;charset=UTF-8;","application/json;"})在@RequestMapping里面加上 produces={ "text/html;charset=UTF-8;","application/json;"}
第三种:
前端js处理,用contentType来处理
jQuery(form).ajaxSubmit({
url: "ajax.aspx?a=memberlogin",
type: "post",
dataType: "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success: showLoginResponse
});