Home > Back-end >  Spring Bad Request for DELETE function
Spring Bad Request for DELETE function

Time:11-18

I am trying to send a list of ids from the frontend to the backend to delete.

Frontend:

var ids = [1,2,3];
var dataIds = {ids: ids};
              $.ajax({
                  type: 'DELETE'
                  , url: _host   '/delete'
                  , data:  dataIds
                  , contentType: 'application/json; charset=utf-8'
                  , dataType: 'application/json'
                  , async: false
                  , success: function (result) {                        
                  }
              });

Backend:

@DeleteMapping(value = "/delete")
    public @ResponseBody String delete(@RequestBody List <Long> ids){
      ...
    }

But the error is :

error: "Bad Request" message: "Required List parameter 'ids' is not present"

Please help. Thanks.

CodePudding user response:

You are sending object with list property but expect just a list.

So you either change front-end part from

$.ajax({ /* omitted */, data: dataIds, /* omitted */ });

to

$.ajax({ /* omitted */, data: ids, /* omitted */ });

or change back-end part to

@DeleteMapping(value = "/delete")
public @ResponseBody String delete(@RequestBody ListWrapper body){
   List<Long> ids = body.ids;
   ...
}

class ListWrapper {
    List<Long> ids;
}

CodePudding user response:

Your delete methods expects an array of numbers, e.g. {1, 2, 3}, but you are submitting { "id": [1, 2, 3] } instead.

  • Related