9

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.

Matt Huggins
  • 81,398
  • 36
  • 149
  • 218

4 Answers4

7

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.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Thanks, Nick. Any suggestions on a workaround? Given your response, I attempted to update `settings.url` in the `$.ajaxSend()` callback such that I append the query string values there for DELETE requests; however, it looks like the returned value of `settings.url` isn't being captured for use in jQuery 1.4.3 as it is in 1.3.2. Any other thoughts on dealing with this, other than manually appending the same parameter to every single request everywhere in my code? – Matt Huggins Oct 25 '10 at 21:43
  • 1
    @Matt - I think 1.4.4 is due out very soon as a bugfix release, look at this commit an hour ago labeling it 1.4.4pre: http://github.com/jquery/jquery/commit/9b97599fa4d615a91d1605d9c664c50f576911ce I would say hang tight a few days, grab 1.4.4 final and you're all set. Here's a blog post on it: http://blog.jquery.com/2010/10/24/community-updates-2610/ – Nick Craver Oct 25 '10 at 21:48
  • Unfortunately deadlines force me to deal with this now. I guess I'll update the calls inline for now, and refactor in a couple days when this becomes available. Thanks so much for your insight, you're always incredibly helpful on here! – Matt Huggins Oct 25 '10 at 21:52
  • The answer is a little outdated. jQuery appends parameters to `GET` and `HEAD`, and for `DELETE` it gives you freedom how to deal with it. For more details see my response. – Robert Zaremba Sep 18 '13 at 12:36
1

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'
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

jQuery doesn't assume how to deal with content for DELETE request. The RFC also doesn't specify this.

There were few issues about it. There were attempts to implement behaviour as in other web tools - to treat DELETE content as a query part of URI.
The final statement is that developer decide what to do with DELETE content. jQuery provides sufficient tools for that. For more check discussion in $.ajax DELETE request not passing data parameters issue.

About other methods, jQuery adds a request content to GET and HEAD. Check rnoContent variable in jQuery source.

Robert Zaremba
  • 8,081
  • 7
  • 47
  • 78
0

This worked for me:

jQuery

$("#DeleUser").click(function () {
    $.ajax({ type: "DELETE", url: userController + "DeleteUser/" + $("#UserId").val() + "?UserPhoto=" + $("#UserPhoto").val() })
    .done(function (data) {
        $('#MsgUser').text("User deleted");
        $("#User").trigger("reset");
    })
    .fail(function (response, status, error) {
        $('#MsgUser').text(error);
    });
});

WebAPI Controller/Action

    public IHttpActionResult DeleteUser(int id)
    {
        try
        {
            var request = HttpContext.Current.Request;
            string userPhoto = request.QueryString["UserPhoto"];
            Users user = new Users();
            user.UserId = id;
            user.Delete(ConfigurationManager.ConnectionStrings["DBName"].ConnectionString, id);
            if (File.Exists(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PhotosPath"]) + userPhoto))
            File.Delete(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PhotosPath"]) + userPhoto);
            return Ok();
        }
        catch (Exception e)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();
            responseMessage.StatusCode = HttpStatusCode.InternalServerError;
            responseMessage.ReasonPhrase = "Exception: " + e.Message;
            throw new HttpResponseException(responseMessage);
        }
    }
ASP Force
  • 161
  • 1
  • 6