Browsers支持PUT
and DELETE
,但它是HTML事实并非如此。
例如,浏览器将启动PUT
通过 Javascript (AJAX) 请求,但不通过 HTML<form>
提交。
这是因为 HTML 4.01 和最终的 W3C HTML 5.0 规范都规定,它们的唯一 HTTP 方法form
允许的元素是 GET 和 POST。
在 HTML 5 的开发过程中对此进行了很多讨论,并且一度将它们添加到 HTML 5 中,但又被删除。从 HTML 5 规范中删除附加方法的原因是 HTML 4 级别的浏览器永远无法支持它们(在它们创建时不是 HTML 的一部分);如果没有 JavaScript shim,他们就无法做到这一点;因此,您不妨使用 AJAX。
尝试使用表单的网页method="PUT"
or method="DELETE"
would 回退到默认方法,GET
适用于所有当前浏览器。这会破坏 Web 应用程序在 HTML 表单中使用适当方法来执行预期操作的尝试,并最终给出更糟糕的结果 —GET
被用来删除东西! (你好爬虫。哦,哎呀!我的数据库消失了)
更改 HTML 的默认方法<form>
元素到POST
会有所帮助(国际海事组织默认应该一直是POST
,自从 Moasic* 于 1993 年首次推出表单以来),但要更改默认值至少需要十年时间才能渗透到安装基础中。简而言之,就是两个词:“因为遗产”。 :-(
To support current browsers, authors will have to fake it with an override. I recommend authors use the widely knowna http://symfony.com/doc/current/cookbook/routing/method_parameters.html#faking-the-method-with-method, b https://github.com/expressjs/method-override#override-using-a-query-value _method
argument by including <input type=hidden name=_method value=DELETE>
in their HTML; switch the form method to POST
(since the request is unsafe); then add recognition of _method
on the server side, which should then do whatever's necessary to mutate the request and forward it on as if it were a real DELETE request.
另请注意,由于网络浏览器是最终的HATEOAS https://en.wikipedia.org/wiki/HATEOAS客户,他们需要有一个新状态传输给他们以进行 DELETE 请求。现有的 API 经常返回204 No Content
对于此类请求。您应该发回带有链接的超媒体响应,以便用户可以继续浏览器状态。
另请参阅这些相似/相同问题的答案:
- 为什么 HTML 表单上没有 PUT 和 DELETE 方法? https://softwareengineering.stackexchange.com/questions/114156/why-are-there-are-no-put-and-delete-methods-on-html-forms
- PUT、DELETE、HEAD 等方法在大多数 Web 浏览器中都可用吗? https://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers
- 在 HTML 表单中使用 PUT 方法 https://stackoverflow.com/questions/8054165/using-put-method-in-html-form
- 浏览器是否支持带有多部分/表单数据的 PUT 请求 https://stackoverflow.com/questions/23748551/do-browsers-support-put-requests-with-multipart-form-data
* Mosaic, created by Marc Andreessen, also introduced the compound mistake of the
<img src=…>
tag — it should have been
<image source=…>fallback</image>
.