向后台请求短信验证码(发送短信的逻辑已被注释,只要取得6位数字)
注册页面返回的是乱码+我要的结果,响应头是:Content-Type:image/jpeg。
其他请求都是正常返回ContentType:application/json;charset=UTF-8
注册页面:
$.ajax({ type : "POST", url : "/getPhoneCode", cache: false, async : false, data : { "tel" : tel,//手机号码 "validateCode" : validateCode,//图片上的数字验证码 "sendBy" : 'reg', }, success : function(result) { message = result.message; if(message == "false"){ errorsTelCode(); }else{ alert(message); } } });
后台:
@ResponseBody @RequestMapping(value = "/getPhoneCode",produces="application/json;charset=UTF-8") public Result main(SmsHistoryView smsHistoryView) throws IOException{ HttpServletRequest request = HttpHelper.getRequest(); String tel = smsHistoryView.getTel(); String sendBy = smsHistoryView.getSendBy(); String validateCode = smsHistoryView.getValidateCode();//获取用户输入的图片上的数字校验码 Result result = new Result(); String apikey = "000"; //修改为您的apikey.apikey可在官网(http://www.yuanpian.com)登录后获取 String mobile = URLEncoder.encode(tel,ENCODING); //修改为您要发送的手机号 long tpl_id = Long.parseLong(App.tplId()); //设置模板ID,如使用1号模板:【#company#】您的验证码是#code# int phone_code = (int)((Math.random()*9+1)*100000); String verifyCode = String.valueOf(phone_code); String tpl_value = URLEncoder.encode("#code#",ENCODING)+ "=" + URLEncoder.encode(verifyCode, ENCODING); String checkcode = (String) request.getSession().getAttribute("checkcode");//获取图片上的数字校验码 //对是否能获取短信进行校验 result.setMessage(tpl_value); if(sendBy.equals("FormReg")){ if(validateCode.equalsIgnoreCase(checkcode)){ DrawImageController DrawImageController = new DrawImageController(); DrawImageController.index(); //发短信逻辑已被注释 memberService.log("获取短信校验码", "注册_成功获取短信校验码",2, HttpHelper.getRequest()); }else{ logger.error(String.format("输入的数字验证码错误,手机号码为:%s", mobile)); } } return result; }
public class Result<T> { public Boolean isSuccess; private String message; private T data; public Result(){ isSuccess = false; } public Result(Boolean success){ isSuccess = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
后台已经打断点调试了的,返回的是正常数据的
DrawImageController DrawImageController = new DrawImageController();
DrawImageController.index();
已处理,就是重新生成图形校验码到内存中导致返回的类型有问题