springboot中异步请求中的ajax

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 "";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值