Why don't browsers support PUT and DELETE requests and when will they?

元气小坏坏 提交于 2019-12-03 06:32:20

问题


I'm seeing many frameworks recently that have decided to "fake" PUT and DELETE requests. Like Ruby on Rails. They seem to be waiting for browsers to catch up. Are they waiting in vain?

Is this even slated to be implemented anywhere?


回答1:


Browsers do support PUT and DELETE, but it's HTML that doesn't.

For example, a browser will initiate a PUT request via Javascript (AJAX), but not via HTML <form> submission.

This is because HTML 4.01 and the final W3C HTML 5.0 spec both say that the only HTTP methods that their form elements should allow are GET and POST.

There was much discussion about this during the development of HTML 5, and at one point they got added to HTML 5, only to be removed again. The reason the additional methods were removed from the HTML 5 spec is because HTML 4-level browsers could never support them (not being part of HTML at the time they were made); and there is no way to allow them to do so without a JavaScript shim; thus, you may as well use AJAX.

Web pages trying to use forms with method="PUT" or method="DELETE" would fall back to the default method, GET for all current browsers. This breaks the web applications' attempts to use appropriate methods in HTML forms for the intended action, and ends up giving a worse result — GET being used to delete things! (hello crawler. oh, whoops! there goes my database)

Changing the default method for HTML <form> elements to POST would help (IMO the default should have always been POST, ever since Moasic* debuted forms in 1993), but to change the default would take at least a decade to percolate through the installed base. So in two words: ‘because legacy’. :-(

To support current browsers, authors will have to fake it with an override. I recommend authors use the widely knowna, b_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.

Note also that, since web browsers are the ultimate HATEOAS client, they need to have a new state to be transferred to them for DELETE requests. existing APIs often return 204 No Content for such requests. You should instead send back a hypermedia response with links so that the user can progress their browser state.

Also see the answers to these similar/identical questions:

  • Why are there are no PUT and DELETE methods on HTML forms?
  • Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
  • Using PUT method in HTML form
  • 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>.

来源:https://stackoverflow.com/questions/16805956/why-dont-browsers-support-put-and-delete-requests-and-when-will-they

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!