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>
相关推荐
sunshine6411 分钟前
【CSS】实现tag选中对钩样式
前端·css·css3
真滴book理喻24 分钟前
Vue(四)
前端·javascript·vue.js
蜜獾云26 分钟前
npm淘宝镜像
前端·npm·node.js
dz88i827 分钟前
修改npm镜像源
前端·npm·node.js
Jiaberrr31 分钟前
解锁 GitBook 的奥秘:从入门到精通之旅
前端·gitbook
程序员_三木1 小时前
Three.js入门-Raycaster鼠标拾取详解与应用
开发语言·javascript·计算机外设·webgl·three.js
顾平安2 小时前
Promise/A+ 规范 - 中文版本
前端
聚名网2 小时前
域名和服务器是什么?域名和服务器是什么关系?
服务器·前端
桃园码工2 小时前
4-Gin HTML 模板渲染 --[Gin 框架入门精讲与实战案例]
前端·html·gin·模板渲染
不是鱼2 小时前
构建React基础及理解与Vue的区别
前端·vue.js·react.js