开发者

$.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',
    typ开发者_运维知识库e: '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.


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.


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'
});


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.


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);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜