Luffy Luffy - 7 days ago 5
Ajax Question

500 Internal Server Error when calling ajax in spring

I am using Spring MVC and I have an AJAX which is used to delete selected user. It's working fine on my local system but when I tried to run the same code on development server I'm getting


500 Internal Server Error


I did google to figure out what is wrong with my code but I'm not able to figure out anything till now. Any help will be appreciated.

AJAX function in my JSP file:

$('.del-btn .userId').click(function(){
var userId = $(this).attr("alt");
var data = 'userId='+ userId;
$.ajax({
type: 'POST',
url: '${pageContext.servletContext.contextPath}/deleteUser',
data: data,
success: function(response) {
$('#submitkpi').submit();
}
});
});


deleteUser
function in Controller:

@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> deleteKpi(@ModelAttribute(value = "userId") String userId, BindingResult result) {

if (!userId.isEmpty()) {
userService.deleteUser(userId);
return Collections.singletonMap("ok", true);
}
return Collections.singletonMap("errorMsg", "Unable to complete your request!");
}

Answer

Can you try this?!

$('.del-btn .userId').click(function(){
    var userId = $(this).attr("alt");

    $.ajax({
        url: 'deleteUser',
        data: ({
                userId : userId,
            }),
        success: function(response) {
            alert(response)
        }
    });
});

Controller

@RequestMapping("/deleteUser")
@ResponseBody
public String deleteKpi(@RequestParam(value = "userId") Long userId,  HttpSession session) {

    if (null != userId) {
        userService.deleteUser(userId);
        return "Ok";
    }
    return "NotOk";
}