613

GET:$.get(..)

POST:$.post()..

What about PUT/DELETE?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
user198729
  • 61,774
  • 108
  • 250
  • 348

14 Answers14

1015

You could use the ajax method:

$.ajax({
    url: '/script.cgi',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 69
    Just a note, if you're using an IIS webserver and the jquery `PUT` or `DELETE` requests are returning 404 errors, you will need to enable these verbs in IIS. I've found this to be a good resource: http://geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-express-for-put-and-delete-verbs.aspx – TimDog Jan 12 '12 at 21:03
  • 22
    BE AWARE: `"The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers."` from: http://api.jquery.com/jQuery.ajax/#options – andilabs Dec 02 '13 at 08:11
  • 25
    @andi As per http://stackoverflow.com/questions/1757187/which-browsers-dont-support-a-http-delete-with-jquery-ajax any browser from IE6 on supports these http methods. Unless you're developing for an *ancient* browser, you can safely use http methods beyond "GET" and "POST". – Mar May 06 '15 at 18:51
  • 2
    Also, [you can't pass in form data](http://stackoverflow.com/questions/15088955/how-to-pass-data-in-the-ajax-delete-request-other-than-headers). It has to go through the URI. – xavier Jun 29 '15 at 19:59
  • 6
    for versions after 1.9 you can use `method` or `type` – sites Aug 23 '15 at 04:09
  • just inlining @xavier's note regarding passing data via URI, you would have to specify the data as query-string like so `url: urlCall + '?' + $.param({"Id": Id, "bolDeleteReq" : bolDeleteReq})` (see the link in his comment for the full example). – chamberlainpi Feb 10 '17 at 02:31
135

$.ajax will work.

$.ajax({
   url: 'script.php',
   type: 'PUT',
   success: function(response) {
     //...
   }
});
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 4
    PUT is need `contentType: "application/json"` – KingRider Jul 22 '16 at 13:45
  • 3
    Is there any difference between this answer and Darin Dimitrov's? I assume they were both created at the same time, and therefore there wasn't any plagiarism, but I don't see what this answer adds (apart from 940 reputation to Jacob). – Andrew Grimm Feb 16 '17 at 05:06
79

We can extend jQuery to make shortcuts for PUT and DELETE:

jQuery.each( [ "put", "delete" ], function( i, method ) {
  jQuery[ method ] = function( url, data, callback, type ) {
    if ( jQuery.isFunction( data ) ) {
      type = type || callback;
      callback = data;
      data = undefined;
    }

    return jQuery.ajax({
      url: url,
      type: method,
      dataType: type,
      data: data,
      success: callback
    });
  };
});

and now you can use:

$.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){
   console.log(result);
})

copy from here

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
  • Delete doesn't expect *data* while put does, not to say that $.get and $.post can have different signatures while here you are hardcoding it to one – Francisco Presencia Aug 30 '16 at 11:33
  • 2
    @FranciscoPresencia - 1. Delete doesn't expect data while put does ----> The third line handles this scenario 2. $.get and $.post can have different signatures ----> This is only creating additional jquery methods for delete and put. get and post have their own jquery methods. – Mahesh Jan 10 '18 at 03:02
31

Seems to be possible with JQuery's ajax function by specifying

type: "put" or type: "delete"

and is not not supported by all browsers, but most of them.

Check out this question for more info on compatibility:

Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
11

From here, you can do this:

/* Extend jQuery with functions for PUT and DELETE requests. */

function _ajax_request(url, data, callback, type, method) {
    if (jQuery.isFunction(data)) {
        callback = data;
        data = {};
    }
    return jQuery.ajax({
        type: method,
        url: url,
        data: data,
        success: callback,
        dataType: type
        });
}

jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'DELETE');
    }
});

It's basically just a copy of $.post() with the method parameter adapted.

user2503775
  • 4,267
  • 1
  • 23
  • 41
10

Here's an updated ajax call for when you are using JSON with jQuery > 1.9:

$.ajax({
    url: '/v1/object/3.json',
    method: 'DELETE',
    contentType: 'application/json',
    success: function(result) {
        // handle success
    },
    error: function(request,msg,error) {
        // handle failure
    }
});
moodboom
  • 6,225
  • 2
  • 41
  • 45
5

You should be able to use jQuery.ajax :

Load a remote page using an HTTP request.


And you can specify which method should be used, with the type option :

The type of request to make ("POST" or "GET"), default is "GET".
Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
4

ajax()

look for param type

Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

antpaw
  • 15,444
  • 11
  • 59
  • 88
3

For brevity:

$.delete = function(url, data, callback, type){

  if ( $.isFunction(data) ){
    type = type || callback,
    callback = data,
    data = {}
  }

  return $.ajax({
    url: url,
    type: 'DELETE',
    success: callback,
    data: data,
    contentType: type
  });
}
Sam
  • 4,437
  • 11
  • 40
  • 59
Paul Wand
  • 323
  • 5
  • 12
2

If you need to make a $.post work to a Laravel Route::delete or Route::put just add an argument "_method"="delete" or "_method"="put".

$.post("your/uri/here", {"arg1":"value1",...,"_method":"delete"}, function(data){}); ...

Must works for others Frameworks

Note: Tested with Laravel 5.6 and jQuery 3

Marcos Regis
  • 814
  • 9
  • 13
1

I've written a jQuery plugin that incorporates the solutions discussed here with cross-browser support:

https://github.com/adjohnson916/jquery-methodOverride

Check it out!

AndersDJohnson
  • 131
  • 1
  • 8
1

You can do it with AJAX !

For PUT method :

$.ajax({
  url: 'path.php',
  type: 'PUT',
  success: function(data) {
    //play with data
  }
});

For DELETE method :

$.ajax({
  url: 'path.php',
  type: 'DELETE',
  success: function(data) {
    //play with data
  }
});
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
Xanarus
  • 763
  • 1
  • 6
  • 18
0

CRUD

this may make more sense

CREATE (POST)Request

function creat() {
  $.ajax({
    type: "POST",
    url: URL,
    contentType: "application/json",
    data: JSON.stringify(DATA1),
    success: function () {
      var msg = "create successful";
      console.log(msg);
      htmlOutput(msg);
    },
  });
}

READ (GET)Request

// GET EACH ELEMENT (UNORDERED)
function read_all() {
  $.ajax({
    type: "GET",
    url: URL,
    success: function (res) {
      console.log("success!");
      console.log(res);
      htmlOutput(res);
    },
  });
}

// GET EACH ELEMENT BY JSON
function read_one() {
  $.ajax({
    type: "GET",
    url: URL,
    success: function (res) {
      $.each(res, function (index, element) {
        console.log("success");
        htmlOutput(element.name);
      });
    },
  });
}

UPDATE (PUT)Request

function updat() {
  $.ajax({
    type: "PUT",
    url: updateURL,
    contentType: "application/json",
    data: JSON.stringify(DATA2),
    success: function () {
      var msg = "update successful";
      console.log(msg);
      htmlOutput(msg);
    },
  });
}

DELETE (DELETE)Request

function delet() {
  $.ajax({
    type: "DELETE",
    url: deleteURL,
    success: function () {
      var msg = "delete successful";
      console.log(msg);
      htmlOutput(msg);
    },
  });
}

GitHub Reference

-1

You could include in your data hash a key called: _method with value 'delete'.

For example:

data = { id: 1, _method: 'delete' };
url = '/products'
request = $.post(url, data);
request.done(function(res){
  alert('Yupi Yei. Your product has been deleted')
});

This will also apply for

mumoc
  • 41
  • 4