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>
相关推荐
brief of gali4 分钟前
记录一个奇怪的前端布局现象
前端
前端拾光者43 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
Json_181790144801 小时前
电商拍立淘按图搜索API接口系列,文档说明参考
前端·数据库
风尚云网1 小时前
风尚云网前端学习:一个简易前端新手友好的HTML5页面布局与样式设计
前端·css·学习·html·html5·风尚云网
木子02041 小时前
前端VUE项目启动方式
前端·javascript·vue.js
GISer_Jing1 小时前
React核心功能详解(一)
前端·react.js·前端框架
捂月2 小时前
Spring Boot 深度解析:快速构建高效、现代化的 Web 应用程序
前端·spring boot·后端
深度混淆2 小时前
实用功能,觊觎(Edge)浏览器的内置截(长)图功能
前端·edge
Smartdaili China2 小时前
如何在 Microsoft Edge 中设置代理: 快速而简单的方法
前端·爬虫·安全·microsoft·edge·社交·动态住宅代理
秦老师Q2 小时前
「Chromeg谷歌浏览器/Edge浏览器」篡改猴Tempermongkey插件的安装与使用
前端·chrome·edge