SSM发送手机验证码——以网建SMS为例

整理一下从大二开始写的东东


后台源码链接: 点我自取

到网建申请一个帐号,好像可以免费用10条短信

在这里插入图片描述

设置用户名、密钥,在发送的时候需要携带该信息

前端代码简化后如下,提交表单我用的是ajax,如果直接提交表单记得name属性值要和后台的@RequestParam(“tel”)中的tel属性值一样,后台代码顶部链接自取。

<form role='form' class='login' id='getPswForm'>			 
  <input type='text' name='tel' placeholder='输入手机号码...'class='form-control' id='getTel' />
  <input type='text' name='psw' placeholder='输入新密码...'class='form-control' id='get-psw' />
   <input type='text' name='code' placeholder='输入验证码...'class='form-control' id='code' style='width: 340px;'/>
  <button id='send' class='btn btn-primary mybtn'>点击发送</button>
  </form>

//发送验证码
	$("#send").click(function(){
		var tel = $("#getTel").val();
		if(checkTel("#get-chk-tel",tel)){
			//alert($("#getTel").val());
			$.ajax({
			//这里的url和type和后台的 @RequestMapping(value="send",method=RequestMethod.POST)保持一致
			  type:"POST",
			  data:{tel:$("#getTel").val()},
			  async:false,
			  url:"send",
			  success:function(result){
				 // alert(result);
				  console.log(typeof(result));
				  if(result=="200"){
					  alert("发送成功,注意查收");
					  $("#get-chk-tel").html("");
					  checkCode(60);
				  }else if(result=="2"){
					  $("#get-chk-tel").html("该用户不存在");
					  checkCode(2);
				  }
			  },
			  error:function(xhr,result){
				  alert(result);
			  }
			});
		}else{
			alert("请填写好手机号码");
		}
	});
	function checkCode(time){
	if(time == 0){
		$('#send').removeAttr("disabled");//这个表示,从xx中移除yy属性,$(xx).remove(),移除xx元素
		$('#send').html("重新发送");
	}else{
		time--;
		$('#send').attr("disabled",true);
		$('#send').html("倒计时"+time+"s");
		setTimeout(function(){
		   checkCode(time);
		},1000);
	  }
  }

效果如下:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

legendaryhaha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值