$ .ajax忽略DELETE请求的数据参数

我刚刚从jQuery 1.3.2更新到1.4.3,我在制作AJAX DELETE请求时看到了一些新的行为。 由于某种原因,我的data参数中传递的data未发送到服务器。 例如:

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

结束向/example发送DELETE请求而没有其他数据。 但是,这种类型的调用传递参数就好了:

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

还有其他人见过类似的行为吗? 有没有理由不再工作(即:是设计还是错误)? 有关如何使其工作的任何建议?

另外,如果有人想知道为什么我不想简单地将参数作为URL字符串的一部分传递,那是因为我最终尝试使用$.ajaxSetup回调,在那里提供一些通用参数(即authenticity_token参数)用于防止Rails中的伪造)。 在尝试jQuery 1.4.3之前,这一切都运行良好。

jQuery 只会将参数附加到查询字符串中,仅用于GET请求 (并且没有DELETE请求的主体 ),所以这是jQuery 1.4.3中的故意行为。

但是,从那时起( 在此提交 )允许在1.4.4版本中允许正文进行DELETE请求。

这可能与traditional参数有关吗? 它通常与复杂类型有关,而不是简单的id参数,但值得检查是否不是这种情况:

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

jQuery没有假设如何处理DELETE请求的内容。 RFC也没有指定这一点。

关于它的问题很少。 尝试像其他Web工具一样实现行为 – 将DELETE内容视为URI的查询部分。
最后的陈述是开发人员决定如何处理DELETE内容 。 jQuery为此提供了足够的工具。 有关$ .ajax中的更多检查讨论DELETE请求不传递数据参数问题。

关于其他方法,jQuery向GETHEAD添加请求内容。 检查jQuery源代码中的rnoContent变量。

这对我有用:

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控制器/动作

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