-2

I am using a public api for testing that responds to get, post, put, and delete and when attempting to create a http-delete request using jQuery.ajax() my response is undefined. This seems strange as I have successfully received a response using Javascript Fetch to make the request. Is my ajax http-delete request malformed?

Input

$.ajax({
  url: "https://reqres.in/api/users/2",
  type: "DELETE",
  success: function (response) {
    console.log("Success: " + response);
  },
  error: function (response) {
    console.log("Failure: " + response);
  },
});

Output

Success: undefined

Things I've Tried

  • changing property 'type' to 'method'
  • added a property 'error' but the logic goes to success with no response.
  • googling various ajax delete requests and trying those with the reqres.in API
  • successfully got a response using http-delete request with javascript fetch using the reqres.in api
  • successfully got a responses on put, post, and get requests using ajax
  • using different browsers (ie. chrome, edge, firefox)
  • Does this answer your question? [What is passed to the success function in an Ajax delete request?](https://stackoverflow.com/questions/37355598/what-is-passed-to-the-success-function-in-an-ajax-delete-request) – Peter B Apr 12 '21 at 14:37
  • What should the response be, if not undefined? Why would this be an indication of a problem? – Kevin B Apr 12 '21 at 14:39
  • @KevinB When using fetch I am able to use the response.ok to test whether the response was successful or not (while this maybe redundant, was trying to have the same input and output for ajax requests and fetch requests) . I guess if it is actually successful and is normal to respond with nothing on success then okay. Next step I guess is to setup an actual back-end and delete something and see if it deletes and doesn't respond, just to verify this is normal. – Nicholas Deckard Apr 12 '21 at 14:53
  • @NicholasDeckard Fetch and jquery's ajax don't at all present the result to you in the same way. jQuery does the statuscode/success check for you. – Kevin B Apr 12 '21 at 14:54

1 Answers1

0

The DELETE endpoint(https://reqres.in/api/users/2) returns a status code of 204.

This status code indicates the following: 204 No Content(Docs). This means that the action returns no content in response of the DELETE.

This causes the response value to be empty.

Request

The response:

Response

MaartenDev
  • 5,631
  • 5
  • 21
  • 33