ajax请求成功但不执行success-function回调函数

目录

一、问题分析

二、处理问题


一、问题分析

二、处理问题

  • 控制器少了@RequestBody
  • @ResponseBody 注解用于直接将方法返回的对象序列化为 JSON 或其他格式的数据返回给客户端。
  • 部分代码如下

控制器

java 复制代码
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation("注册")
    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public CommonResult register(@RequestBody User user,HttpServletRequest request,HttpServletResponse response){
        CommonResult commonResult = new CommonResult();
        //校验参数是否为空
        if(user == null || StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            commonResult.setCode(CommonCode.FAIL);
            commonResult.setMsg("用户名和密码不能为空!!");
            return commonResult;
        }
        User userByUsername = userService.getUserByUsername(user.getUsername());
        if(userByUsername != null){
            commonResult.setCode(CommonCode.FAIL);
            commonResult.setMsg("用户名已经存在!!");
        }else{
            //更新新用户创建时间
            user.setCreatetime(new Date());
            //存储加密后的新用户信息
            int result = userService.addUser(user);
            if(result == 1){
                commonResult.setCode(CommonCode.SUCCESS);
            }else{
                commonResult.setCode(CommonCode.FAIL);
                commonResult.setMsg("系统原因,请重试或联系管理员!!");
            }
        }
        return commonResult;
    }
}

结果封装类

java 复制代码
package com.study.hrms.common;
import java.util.List;

/**
 * 用于封装接口返回结果的通用格式,包括状态码,消息和数据
 */
public class CommonResult {

    public int code;//状态码
    public String msg;
    public Object data;

    /**
     * 不同参数的构造函数
     */
    public CommonResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public CommonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult() {
    }

    /**
     * 静态方法,创建对象并返回结果的通用格式
     */
    public static <T extends Object> CommonResult success(List<T> list){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonCode.SUCCESS);
        commonResult.setData(list);
        return commonResult;
    }

    public static <T extends Object> CommonResult success(Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonCode.SUCCESS);
        commonResult.setData(data);
        return commonResult;
    }
    public static <T extends Object> CommonResult success(){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonCode.SUCCESS);
        return commonResult;
    }
    public static CommonResult fail(String msg){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonCode.SUCCESS);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

注册页面

html 复制代码
<!DOCTYPE html>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ include file="common/taglibs.jsp" %>

<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>员工管理系统-注册</title>
    <%@ include file="common/meta.jsp" %>
    <link rel="stylesheet" href="/static/css/views/login.css" media="all"/>
</head>
<body>
<!-- Head -->
<!-- Head End -->

<!-- Carousel -->
<!-- Carousel End -->

<!-- Footer -->
<!-- Footer End -->



<!-- LoginForm -->
<div class="login_main">
    <fieldset class="layui-elem-field layui-field-title hazel_mar_02">
        <legend>员工管理系统注册</legend>
    </fieldset>
    <div class="layui-row layui-col-space15">
        <form class="layui-form hazel_pad_01" action="">
            <div class="layui-col-sm12 layui-col-md12">
                <div class="layui-form-item">
                    <input type="text" name="username" lay-verify="required|userName" autocomplete="off" placeholder="账号" class="layui-input">
                    <i class="layui-icon layui-icon-username login_icon"></i>
                </div>
            </div>
            <div class="layui-col-sm12 layui-col-md12">
                <div class="layui-form-item">
                    <input type="password" name="paword" lay-verify="required|pass" autocomplete="off" placeholder="密码" class="layui-input">
                    <i class="layui-icon layui-icon-password login_icon"></i>
                </div>
            </div>
            <div class="layui-col-sm12 layui-col-md12">
                <div class="layui-form-item">
                    <input type="password"  lay-verify="required|confirmPass" autocomplete="off" placeholder="确认密码" class="layui-input">
                    <i class="layui-icon layui-icon-password login_icon"></i>
                </div>
            </div>
            <div class="layui-col-sm12 layui-col-md12">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="go">注册</button>
            </div>
            <div class="layui-form-mid layui-word-aux"><a href="/loginPage">登录</a></div>
            <div class="layui-form-mid layui-word-aux hint">
                <span id="registermsg"></span>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use('form', function () {//加载form模块
        var form = layui.form;
        var $ = layui.$;
        form.verify({
            confirmPass:function(value){
                if($('input[name=paword]').val() !== value){
                    console.log($('input[name=paword]').val()+":"+value);
                    $("#registermsg")[0].innerHTML = '两次密码输入不一致!';
                    return "false";
                }
            }
        });
        //submit登录按钮
        form.on('submit(go)', function (data) {
            var user = {};//构造user对象,传递参数
            user.username = $("input[name=username]").val();
            user.password = hex_md5($("input[name=paword]").val());//密码使用MD5加密
            $.ajax({
                url: "/register",
                type: "POST",
                data: JSON.stringify(user),//传递json类型的参数
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if(result.code == COMMON_SUCCESS_CODE){
                        location.href = '/login'
                    }else{
                        $("#registermsg")[0].innerHTML = result.msg;
                    }
                }
            })
            return false;//拦截layui自带的提交
        })
    });

</script>
</body>
</html>
相关推荐
酷酷的阿云几秒前
不用ECharts!从0到1徒手撸一个Vue3柱状图
前端·javascript·vue.js
微信:137971205872 分钟前
web端手机录音
前端
齐 飞8 分钟前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
神仙别闹25 分钟前
基于tensorflow和flask的本地图片库web图片搜索引擎
前端·flask·tensorflow
aPurpleBerry1 小时前
JS常用数组方法 reduce filter find forEach
javascript
GIS程序媛—椰子1 小时前
【Vue 全家桶】7、Vue UI组件库(更新中)
前端·vue.js
DogEgg_0011 小时前
前端八股文(一)HTML 持续更新中。。。
前端·html
ZL不懂前端1 小时前
Content Security Policy (CSP)
前端·javascript·面试
乐闻x1 小时前
ESLint 使用教程(一):从零配置 ESLint
javascript·eslint
木舟10092 小时前
ffmpeg重复回听音频流,时长叠加问题
前端