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
    评论
在Spring Boot,前台异步请求可以通过使用Ajax来发送请求,并通过回调函数来接收返回信息。 首先,我们可以在前台页面使用jQueryAjax方法发送异步请求。例如,可以使用以下代码发送POST请求: ```javascript $.ajax({ url: '/api/your-url', // 请求的URL地址 type: 'POST', // 请求方式(GET、POST等) dataType: 'json', // 返回的数据类型 data: { // 请求参数 param1: 'value1', param2: 'value2' }, success: function(response) { // 请求成功时的回调函数 // 在此处处理返回的数据(response) }, error: function(xhr) { // 请求失败时的回调函数 // 在此处处理错误信息 } }); ``` 在Spring Boot后台,可以通过在Controller定义一个映射URL来接收前台发送的异步请求,并返回相应的数据。例如,可以使用以下代码: ```java import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api") public class YourController { @PostMapping("/your-url") public YourResponse handleAjaxRequest(@RequestBody YourRequest request) { // 在此处处理请求数据,并生成相应的返回数据 YourResponse response = new YourResponse(); // 设置返回数据的内容 return response; } } ``` 在上述代码,`YourRequest`和`YourResponse`是自定义的请求和响应类,用于封装请求参数和返回数据。 在接收到前台发送的异步请求并处理完毕后,可以通过使用`@ResponseBody`注解将返回的数据转换为JSON格式,并自动发送回前台。在上述示例,我们返回的数据类型是`YourResponse`,该类的内容将序列化为JSON格式返回给前台。 总之,Spring Boot的前台异步请求可以通过使用Ajax发送请求,并在后台Controller处理请求并返回相应的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值