0
$("#btnDel").on("click",function(){
var idsarray = [];
$("input:checkbox[name=delbox]:checked").each(function() {
                idsarray.push($(this).val());
            });
        console.log(idsarray);  
var idsString = idsarray.join();
console.log(idsString);

   $.ajax({
    url: 'StudentController',
    dataType: 'json',
    data: {
        "idsToBeDeleted" : idsString
    },
    type: 'DELETE'
    });
});

this is the JS code , im catching the ids of the checkboxes and sending them as a comma separated string to the servlet . thing is when i try to catch the variable on the servlet , and print it out , all i get is null .

protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //catching values from recieved ajax request
                String idlistString = (String) request.getParameter("idsToBeDeleted");
                System.out.println(idlistString);
                

                response.sendRedirect(request.getContextPath());
    }

servlet code over here , some explanation on how to usually catch the sent data through an ajax request would be appreciated .

1 Answers1

-1

Try with a slash before your url and type: 'POST'. Also notice the small changes I have made.

$.ajax({
    url: '/StudentController',
    type: 'POST',
    dataType: 'json',
    data: {
        operation: 'remove',
        idsToBeDeleted: idsString
    },
    type: 'DELETE'
});
  • im getting the request to the server already , the problem is just that i can't catch the value of the data i'm sending (i the type of request to be delete as well , can't change that anyways ). – dany allaw Sep 16 '21 at 08:53