【问题标题】:Delete from controller using JQUERY AJAX (Spring MVC)使用 JQUERY AJAX (Spring MVC) 从控制器中删除
【发布时间】:2016-05-14 02:57:38
【问题描述】:

有人可以帮我解决这个问题吗?我需要使用这个存储库实现 ajax 、 sweetalert.js :http://t4t5.github.io/sweetalert/

到目前为止,一切都很顺利,可以使用 onclick = "" 并调用我的函数。如果有人能告诉我应该如何使用此功能来消除员工,我将不胜感激。

这是我的控制器方法::

    @RequestMapping(value = "/eliminarEmpleado", method = RequestMethod.GET)
public ModelAndView eliminarEmpleado(HttpServletRequest request) {
    int empId = Integer.parseInt(request.getParameter("id"));
    empleadoService.eliminarEmpleado(empId);
    return new ModelAndView("redirect:/");
}

这是我的 jsp 员工列表和删除按钮所在的位置(我需要替换我进行测试的 href ,这当然工作得很好,但不使用我需要的方式 bone : jquery 。):: :

<table id="central"  class="table table-condensed" align="center">

    <thead >
        <tr>
            <th >ID</th>
            <th>NOMBRE</th>
            <th >AP. PATERNO</th>
            <th>AP. MATERNO</th>
            <th>EMAIL</th>
            <th>TELÉFONO</th>
            <th>FECHA DE NACIMIENTO</th>
            <th>SALARIO</th>
            <th>REGIÓN</th>
            <th>PAÍS</th>
            <th>CALLE</th>
            <th>CÓDIGO POSTAL</th>
            <th>CIUDAD</th>
            <th>PROVINCIA</th>
            <th>DEPARTAMENTO</th>
            <th>ACCIONES</th>
        </tr>
    </thead>
    <tbody>


        <c:forEach items="${lista}" var="r">
            <tr>
                <td id="idEmpleado"  align="center">${r.idEmpleado}</td>
                <td align="center">${r.nombre}</td>
                <td align="center">${r.apPaterno}</td>
                <td align="center">${r.apMaterno}</td>
                <td align="center">${r.email}</td>
                <td align="center">${r.telefono}</td>
                <td align="center">${r.fechaNac}</td>
                <td align="center">${r.salario}</td>
                <td align="center">${r.nombreRegion}</td>
                <td align="center">${r.nombrePais}</td>
                <td align="center">${r.nombreCalle}</td>
                <td align="center">${r.codigoPostal}</td>
                <td align="center">${r.ciudad}</td>
                <td align="center">${r.provincia}</td>
                <td align="center">${r.nombreDepartamento}</td>

                <td><a data-original-title="Ver" href="editContact.htm?id=${empleado.id}" data-toggle="tooltip" data-placement="top" title=""><span class="glyphicon glyphicon-eye-open"></span></a>  <a data-original-title="Eliminar"   href="eliminarEmpleado.htm?id=${r.idEmpleado}" data-toggle="tooltip" data-placement="top" title=""><span class="glyphicon glyphicon-trash"></span></a> </td>
                </tr>
        </c:forEach>

    </tbody>
</table>

这是我的 jquery 代码,他们可以帮助我吗?我需要确切地知道我是如何使用它的,我继续尝试几个小时,但我没有得到我希望的结果:(

<script>


    function deleteEmploy(idEmpleado) {

        swal({   
            title: "Are you sure?",   
            text: "You will not be able to recover this imaginary file!",   
            type: "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            cancelButtonText: "No, cancel plx!",   
            closeOnConfirm: false,   
            closeOnCancel: false
            }, 
            function(isConfirm){   
                if (isConfirm) { 

                    swal("Deleted!", "Your imaginary file has been deleted.", "success");   
                    } else {     swal("Cancelled", "Your imaginary file is safe :)", "error");   
        } });
    event.preventDefault


}
    </script>

【问题讨论】:

    标签: javascript java jquery ajax spring-mvc


    【解决方案1】:

    您缺少ajax 函数调用,它将调用服务器端的 eliminarEmpleado 控制器方法。您也没有在代码中的任何地方调用 deleteEmploy() 。

    试试这个:

    HTML:将id 添加到锚标记,单击该标记应调用 deleteEmploy()

    <td><a data-original-title="Eliminar" data-toggle="tooltip" data-placement="top" title="" id="deleteEmp" ><span class="glyphicon glyphicon-trash"></span></a> </td>
    

    Javascript:将 deleteEmploy() 注册为&lt;a id="deleteEmp"&gt; 链接的onclick 事件处理程序并调用ajax()

    <script>
    
        $("#deleteEmp").on("click", deleteEmploy); //when #deleteEmp link is clicked, deleteEmploy() will be called
    
        function deleteEmploy() {
    
            swal({   
                title: "Are you sure?",   
                text: "You will not be able to recover this emplyoyee!",   
                type: "warning",   
                showCancelButton: true,   
                confirmButtonColor: "#DD6B55",   
                confirmButtonText: "Yes, delete it!",   
                cancelButtonText: "No, cancel plx!",   
                closeOnConfirm: false,   
                closeOnCancel: false
                }, 
                function(isConfirm){   
                    if (isConfirm) { 
                        var data = {};
                        data["idEmpleado"] = $("#idEmpleado").html();
                        $.ajax({
                            type: "GET",
                            contentType: "application/json",
                            url: "${home}/eliminarEmpleado",
                            data: JSON.stringify(data),
                            dataType: "json",
                            success: function(){ 
                                swal("Deleted!", "The employee has been deleted.", "success");   
                            },
                            error: function(){
                                swal("Error", "Could not be deleted! :)", "error");   
                            }
    
                        });  
    
    
    
                  } else {     swal("Cancelled", "Employee is safe :)", "error");   
            } });
        event.preventDefault
    
    
    }
    </script>
    

    【讨论】:

    • 谢谢你的朋友,我会努力的。我会照原样告诉你。
    • 一切都很顺利,朋友非常感谢你,但现在我无法调用我的函数,你知道如何替换 ::: href = "eliminarEmpleado.htm id = $ { r.idEmpleado } ?”由新函数“deleteEmploy()”创建,因为我发送的参数是发送的?
    • 您没有在代码中调用deleteEmploy()。您可以将deleteEmploy 注册为onclick 事件处理程序a 标记 - 这样当点击链接时,deleteEmploy() 被调用并触发 ajax。
    • 哦,谢谢朋友的回答我很珍惜时间,有gmail联系你吗?我看到你很擅长这个我想走上这条路。
    • 朋友只是试着把它放进去,但我得到这个错误函数 :: "GET http : // localhost: 8080 / app / eliminarEmpleado {% 22idEmpleado?{% 22 : % 22 % 22 } 500(内部服务器错误)“当我尝试消除发送 ::: href =”?editContact.htm id = $ { empleado.id }“执行成功删除这条路线投掷自己”http://localhost:8080/app / eliminarEmpleado?id=14" 你认为是什么问题?
    猜你喜欢
    • 2017-12-09
    • 1970-01-01
    • 2018-02-24
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多