0

I have a controller servlet, a jsp and some javascript/jquery code. When the user clicks a 'delete' button, the ajax is sending a request to the servlet, calling the servlet's deleteProject() method, which basically deletes a user's project from the database and the projects jsp page. So far, so good. The problem is that once the delete operation has been completed, the servlet redirects me to the home/login page, instead of staying on the projects page. I'm not sure why this is happening or how I can fix it. Here is the servlet's doPost() method:

             protected void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {

                    RequestDispatcher dispatcher = null;
                    HttpSession session = request.getSession(true);
                    String base = "/jsp/";
                    String url = base + "/home.jsp";
                    String action = request.getParameter("action");

                    if (action != null) {
                        switch (action) {

                        case "Delete":
                            url = base + "projects.jsp";
                            String pid = request.getParameter("parameter_pid");
                            int project_id = Integer.parseInt(pid);
                            User user = (User) session.getAttribute("currentSessionUser");
                            deleteProject(user, project_id);                
                            break; 
     }      
         dispatcher = getServletContext().getRequestDispatcher(url);
                dispatcher.forward(request, response);      
        return;
}

And this is the jQuery code (it displays a modal and if the user confirms deletion, it deletes the project from the database.)

$('#deleteprojectmodal').on('show.bs.modal', function (e) {

    var project_id = e.relatedTarget.id;    
    var project_name = e.relatedTarget.name;     
    $(this).find('.modal-body').html('Delete project: ' + project_name +"?" );    
    $('#parameter_pid').val(project_id); 


    $('#Delete').click (function ()
            {

                $.ajax({
                    url: '/Testing',
                    data: {
                        action: "Delete",
                        parameter_pid : project_id
                    },
                    type: 'post',

                });
            });

    });

My guess is that the "action" parameter is null after the ajax delete operation has completed succesfuly, but I'm not sure how to fix it. All I want is for the user to delete the project from the database and stay on his projects page with the updated projects list.

Any ideas? Many thanks. A.

Edit:

This is the error message that I'm getting at the console of Firefox. Apparently, the ajax function never completes:

Object { readyState: 0, getResponseHeader: getResponseHeader(), getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(), overrideMimeType: overrideMimeType(), statusCode: statusCode(), abort: abort(), state: state(), always: always(), catch: catch(), … }

A_W
  • 43
  • 1
  • 6
  • If a servlet meant to serve as ajax resource it should do any redirection/forward but your servlet do `dispatcher.forward(request, response);`. This is the code make your app "redirection". You can write some sort of completion code to response stream like `response.getWriter().write("ok");` – ST. Kee Jul 20 '18 at 03:26
  • Thank you for your help. After hours of searching, I discovered that the problem was caused by the submit button. Once I changed it to input type="button", the request completed. I know it sounds ridiculous. https://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 – A_W Jul 20 '18 at 10:10

0 Answers0