25

Is there a way to send a DELETE request from a website, using xmlhttprequest or something similar?

Martin Thoma
  • 108,021
  • 142
  • 552
  • 849
Kalvaresin
  • 251
  • 1
  • 3
  • 3

6 Answers6

29

As someone mentioned above, jQuery will do this for you, via the following syntax:

$.ajax({
    type: "DELETE",
    url: "delete_script.php",
    data: "name=someValue",
    success: function(msg){
        alert("Data Deleted: " + msg);
    }
});
Mike Trpcic
  • 24,677
  • 8
  • 74
  • 112
8

You can test if your browse has DELETE implemented here

Supposing req is a XMLHttpRequest object, the code would be req.open("DELETE", uri, false);

jbochi
  • 27,813
  • 15
  • 71
  • 89
6

I use fetch API on one of the projects:

fetch(deleteEndpoint, {
  method: 'delete',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({id: id, token: token})
})

I have deleteEndpoint, id and token previously defined.

Damian Pavlica
  • 26,228
  • 8
  • 66
  • 73
4

This can be done with jQuery, if you don't mind the dependence on a framework. I believe jQuery uses XmlHttpRequest to perform this action. You'd use the $.ajax function with the type parameter set to DELETE.

Please note that not all browsers support HTTP DELETE requests.

Tim S. Van Haren
  • 8,723
  • 2
  • 29
  • 34
0

I believe that both PUT and DELETE are not implemented (in the case of Prototype) due to flaky browser support.

robjmills
  • 18,129
  • 15
  • 73
  • 119
-3

You can use php to do this:

setup your XMLHTTPRequst to call a phpscript that deletes a named file, and then pass the filename that you intend to be removed to the php script and let it do its business.

MikeEL
  • 664
  • 5
  • 13