Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajax ignoring data param for DELETE requests

I just updated from jQuery 1.3.2 to 1.4.3, and I'm seeing some new behavior when making AJAX DELETE requests. For some reason, the data being passed in my data parameter is not being sent to the server. For example:

$.ajax({
    url: '/example',
    data: {id: 12},
    type: 'DELETE'
});

Ends up sending a DELETE request to /example with no additional data. However, this type of call passes the parameters just fine:

$.ajax({
    url: '/example?id=12',
    type: 'DELETE'
});

Has anyone else seen similar behavior? Is there a reason this is no longer working (i.e.: is it by design, or is it a bug)? Any suggestions on how to get it working?

Also, in case anyone is wondering why I don't simply want to pass the parameters as part of the URL string, it's because I'm ultimately attempting to use the $.ajaxSetup callback, providing some general parameters there (namely the authenticity_token parameter used to protect against forgery in Rails). This all worked fine prior to trying jQuery 1.4.3.

like image 239
Matt Huggins Avatar asked Oct 25 '10 21:10

Matt Huggins


People also ask

How to send DELETE request in Ajax?

The working of the ajax delete request So we can use the ajax() function with type option as “$. ajax( 'http://time.jsontest.com', { type : “DELETE});”, where the first parameter is the URL of the data that to delete. So, if the request successful means that the specified data will get deleted.

How to DELETE data using Ajax?

ajax({ type:'POST', url:'delete. php', data:del_id, success: function(data){ if(data=="YES"){ $ele. fadeOut(). remove(); }else{ alert("can't delete the row") } } }) });

How to DELETE data in Ajax jQuery?

You can do this with jQuery AJAX where you need to pass the record id from AJAX which needs to delete. In the example, I am creating the HTML table which shows the list of records with a delete button. When the button gets clicked then remove the record and also remove the HTML table row with fadeOut() effect.


2 Answers

jQuery will only append parameters to the querystring for GET requests only (and no body for DELETE requests), so this is intentional behavior in jQuery 1.4.3.

However, there is a change since then (commit here) to allow a body for DELETE requests in the 1.4.4 release.

like image 112
Nick Craver Avatar answered Sep 21 '22 05:09

Nick Craver


Could this be related to the traditional parameter? It usually relates to complex types and not a simple id parameter but worth checking if this is not the case:

$.ajax({
    url: '/example',
    data: { id: someValue },
    traditional: true,
    type: 'DELETE'
});
like image 35
Darin Dimitrov Avatar answered Sep 21 '22 05:09

Darin Dimitrov