如何解决ajax返回

留给自己看

第一种方法将返回类型设置为对象

先创建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 
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值