Request not found ajax spring mvc

I have a table with people. When I click on the delete icon, I want to remove this person from the database and successfully delete the row from the table. I keep getting it deleteEmployee?id=37 not found 404. I have a controller with @RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET). The person I clicked was deleted from the database (checked), so the controller should be fine. But why am I getting an error message?

@RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET)
public void deleteEmployee(@RequestParam(value = "id", required = true) int id) {
    System.out.println(id);
    employeeDAO.deleteEmployee(id);
}


$(document).on('click','.delete-emp', function(){
    deleteEmployee(this);
});
function deleteEmployee(el){
    var id = $(el).parent().attr('data');
    console.log("delete: "+id);
    $.ajax({
        url: "deleteEmployee?id="+id,
        success: function(){
            deleteRow(id);
        }
    });
}

function deleteRow(el){
    var row = $('.employee-row[data='+el+']');
    var shiftRow = row.next();
    console.log("deleting "+row+" "+shiftRow);
    row.remove();
    shiftRow.remove();
}
0
source share
1 answer

Using annotaion @ResponseBody in your method is enough. this will solve your problem.

@RequestMapping(value = "/android/api/home", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void Home(Locale locale, Model model,HttpServletRequest request,HttpServletResponse response) throws IOException {
    //your logic
}

add content type to ajax call

jQuery
        .ajax({
            url : controllerUrl,
            data : oMyForm,
            dataType : 'text',
            processData : false,
            contentType : false,
            type : 'POST',
            success : function(data) {

            }
        });
+2
source

Source: https://habr.com/ru/post/1652753/


All Articles