用jQueryAjax序列化表单后台取不到数据问题

Ajax后台取不到数据问题

前端代码

<form  onsubmit="return modifyEmployees()" id="modifyEmployees" >
            <table class="layui-table">
                <tr style="border: 0px">
                    <td>员工编码</td>
                    <td>
                        <input name="e_Code" class="layui-input" type="text" th:value="${newEmployee.e_Code}"/>
                    </td>
                </tr>
                <tr>
                    <td>员工姓名</td>
                    <td>
                        <input name="e_RealName" class="layui-input" type="text" th:value="${newEmployee.e_RealName}"/>
                    </td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" name="e_Sex.sex_Id" value="1" id="male" th:checked="${newEmployee.e_Sex.sex_Id eq 1}"/>
                        <label for="male">男</label>
                        <input type="radio" name="e_Sex.sex_Id" value="2" id="female" th:checked="${newEmployee.e_Sex.sex_Id eq 2}"/>
                        <label for="female">女</label>
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" name="e_Status.st_Id" value="1" id="normal" th:checked="${newEmployee.e_Status.st_Id eq 1}"/>
                        <label for="normal">正常</label>
                        <input type="radio" name="e_Status.st_Id" value="2" id="disable" th:checked="${newEmployee.e_Status.st_Id eq 2}"/>
                        <label for="disable">禁用</label>
                    </td>
                </tr>
                <tr>
                    <td>所属部门</td>
                    <td>
                        <select name="e_DeptId.d_Id" class="layui-select" >
                            <option value="0">--请选择--</option>
                            <option th:selected="${newEmployee.e_DeptId.d_Id eq dept.d_Id}" th:each="dept:${depts}"
                                    th:value="${dept.d_Id}" th:text="${dept.d_Name}"></option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>角色身份</td>
                    <td th:text="${newEmployee.e_Role.r_Name}"></td>
                </tr>
                <tr>
                    <td>备注信息</td>
                    <td>
                        <textarea name="e_Description" class="layui-textarea" th:value="${newEmployee.e_Description}"></textarea>
                    </td>
                </tr>
                <input name="e_Id" type="hidden" th:value="${newEmployee.e_Id}"/>
                <tr>
                    <td colspan="2" style="text-align: center">
                        <input type="submit" value="提交" class="layui-btn layui-bg-blue"/>
                        <input type="button" value="返回" class="layui-btn layui-bg-blue" onclick="back()"/>
                    </td>
                </tr>
            </table>
            </form>

Ajax代码

function modifyEmployees() {
    var emp = ($("#modifyEmployees").serialize());
    $.ajax({
        //几个参数需要注意一下
        type: "POST",//提交类型
        contentType:'application/json;charset=UTF-8',
        dataType: "json",//预期服务器返回的数据类型
        url: "/modifyEmployees" ,//url
        data: emp,//序列化表单信息
        success: function (data) {
            if (data == true) {
                layer.msg("修改成功!");
                parent.$("#employees").DataTable().ajax.reload();
            }else{
                layer.msg("修改失败!");
            }
        }
    });
    return false;
}

后台返回的信息
在这里插入图片描述

就是后台取不到值,很烦!找了半天原因是因为Ajax,contentType的值:
contentType:‘application/json;charset=UTF-8’,
这个类型的值填错了应该改为:
contentType:‘application/x-www-form-urlencoded’,
这样就可以了;

完!

猜你喜欢

转载自blog.csdn.net/weixin_43538859/article/details/84987090