skip to Main Content

Getting an error when trying to delete a user from db.
Here is the code that i wrote:
This is server side:

@RestController
public class EmployeeRestController {

   @DeleteMapping( value = "/delete_user")
   public List<Employee> deleteEmployee(@RequestParam(value = "id") Integer id) {
       employeeService.deleteEmployee(id);
       List<Employee> list = employeeService.getAllEmployees();
       return list;
   }
 }

Client side:

function delete_row(id){
    $.ajax({
        type:'DELETE',
        url:'/delete_user',
        dataType: 'json',
        data:JSON.stringify({
            id:id
            }),
        success:function(data){
            console.log(data);
        }
    });
}

Error from server side :

DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'id' is not present]

Error code from client side is 400

I am noob at ajax , javascript and spring. Sorry if the problem is obvious and thanks in advance.

2

Answers


  1. Try using PathVariable instead of RequestParam.

        @DeleteMapping(value = "/delete_user/{id}")
       public List<Employee> deleteEmployee(@PathVariable("id") Integer id) {
           employeeService.deleteEmployee(id);
           List<Employee> list = employeeService.getAllEmployees();
           return list;
       }
     }
    
    Login or Signup to reply.
  2. did you tried this

    function delete_row(id){
    $.ajax({
                    type: "DELETE",
                    url: "/delete_user",
                    data: { id: id },
                    success: function (data) {
                        console.log(data);
                    },
                    error: function () {
                        
                    }
                });
    }
    

    or directly bind to url

    function delete_row(id){
    $.ajax({
                    type: "DELETE",
                    url: "/delete_user?id="+id,
                    success: function (data) {
                        console.log(data);
                    },
                    error: function () {
                        
                    }
                });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search