html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>注册</title> <!--<link rel="stylesheet" href="layui/css/layui.css"> <script src="layui/layui.js"></script>--> <script type="text/javascript" src="js/jquery-1.11.3.min.js" ></script> </head> <body style="width: 100%;height: 100%;"> <div style="margin-top: 15%;"> <div style="width: 700px;"> <h1 style="text-align: center">请输入注册信息</h1> <form > 邮箱:<input type="email"><br> 密码:<input type="password"><br> <!-- <input id="btn1" type="submit" value="提交">--> </form> <button οnclick="test()" id="btn1">提交解决</button> </div> </div> <script type="text/javascript"> function test(){ alert("======") $.ajax({ url:"getCheckCode", type:"get", data:{ email:'123' }, dataType:"json", success: function(data){ //请求成功后的回调函数,data为返回的数据 console.log(data); } }) } </script> </body> </html>
Controller
package com.example.test.controller; import com.example.test.service.mailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Random; @Controller public class MailController { @Autowired(required = false) private mailService mail; // @RequestMapping("getCheckCode/{email}") @PathVariable(name="email") // @RequestMapping("getCheckCode") @GetMapping("getCheckCode") @ResponseBody public Str
package com.example.test.controller; import com.example.test.service.mailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Random; @Controller public class MailController { @Autowired(required = false) private mailService mail; // @RequestMapping("getCheckCode/{email}") @PathVariable(name="email") // @RequestMapping("getCheckCode") @GetMapping("getCheckCode") @ResponseBody public String getCheckCode(String email){ String checkCode = String.valueOf(new Random().nextInt(899999) + 100000); System.out.println("================="); System.out.println(email); System.out.println("================="); System.out.println(checkCode); System.out.println("================="); String message = "您的注册验证码为:"+checkCode; try { mail.sendSimpleMail(email, "注册验证码", message); }catch (Exception e){ return ""; } return checkCode; } @RequestMapping("hahaha") public String hahaha(){ return "HaHaHa"; } @RequestMapping("register") public String register(String email,String password,String checkCode){ System.out.println(email+"==="+password+"===="+checkCode); return ""; } }