64

Below is my Ajax request for a DELETE request:

deleteRequest: function (url, Id, bolDeleteReq, callback, errorCallback) {
    $.ajax({
        url: urlCall,
        type: 'DELETE',
        headers: {"Id": Id, "bolDeleteReq" : bolDeleteReq},
        success: callback || $.noop,
        error: errorCallback || $.noop
    });
}

Is there any alternative way to pass the data other than in the headers?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Prats
  • 1,745
  • 4
  • 24
  • 28

3 Answers3

105

Read this Bug Issue: http://bugs.jquery.com/ticket/11586

Quoting the RFC 2616 Fielding

The DELETE method requests that the origin server delete the resource identified by the Request-URI.

So you need to pass the data in the URI

$.ajax({
    url: urlCall + '?' + $.param({"Id": Id, "bolDeleteReq" : bolDeleteReq}),
    type: 'DELETE',
    success: callback || $.noop,
    error: errorCallback || $.noop
});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

I was able to successfully pass through the data attribute in the ajax method. Here is my code

$.ajax({
     url: "/api/Gigs/Cancel",
     type: "DELETE",
     data: {
             "GigId": link.attr('data-gig-id')
           }

  })

The link.attr method simply returned the value of 'data-gig-id' .

dinith jayabodhi
  • 531
  • 2
  • 8
  • 19
0

Playing with data in AJAX request with DELETE type will take up a lot of your time trying to deal with Uncaught TypeError: Cannot read properties of undefined (reading 'data').

A good oldschool approach (pass the data in the URI as well as you do for GET request) will save your time:

let requestUrl = urlCall + '?Id=' + Id + '&bolDeleteReq=' + bolDeleteReq;

$.ajax({
url: requestUrl,
type: 'DELETE'  
   
Viktor
  • 380
  • 5
  • 14