I've seen a couple questions around here like How to debug RESTful services, which mentions:
我在这里看到了一些问题,比如如何调试RESTful服务,其中提到:
Unfortunately that same browser won't allow me to test HTTP PUT, DELETE, and to a certain degree even HTTP POST.
不幸的是,相同的浏览器不允许我测试HTTP PUT、DELETE和某种程度的HTTP POST。
I've also heard that browsers support only GET and POST, from some other sources like:
我还听说浏览器只支持GET和POST,从其他一些来源,比如:
- http://www.packetizer.com/ws/rest.html
- http://www.packetizer.com/ws/rest.html
- http://www.mail-archive.com/jmeter-user@jakarta.apache.org/msg13518.html
- http://www.mail-archive.com/jmeter-user@jakarta.apache.org/msg13518.html
- http://www.xml.com/cs/user/view/cs_msg/1098
- http://www.xml.com/cs/user/view/cs_msg/1098
However, a few quick tests in Firefox show that sending PUT
and DELETE
requests works as expected -- the XMLHttpRequest
completes successfully, and the request shows up in the server logs with the right method. Is there some aspect to this I'm missing, such as cross-browser compatibility or non-obvious limitations?
但是,Firefox中的一些快速测试显示,发送PUT和DELETE请求与预期一样工作——XMLHttpRequest成功完成,请求用正确的方法显示在服务器日志中。我是否遗漏了一些方面,比如跨浏览器兼容性或不明显的限制?
7 个解决方案
#1
431
HTML forms (up to HTML version 4 and XHTML 1) only support GET and POST as HTTP request methods. A workaround for this is to tunnel other methods through POST by using a hidden form field which is read by the server and the request dispatched accordingly.
HTML表单(直到HTML版本4和XHTML 1)只支持GET和POST作为HTTP请求方法。解决此问题的方法是通过POST方法通过POST方法来使用隐藏的表单字段,该字段由服务器读取,并相应地发送请求。
However, GET, POST, PUT and DELETE are supported by the implementations of XMLHttpRequest (i.e. AJAX calls) in all the major web browsers (IE, Firefox, Safari, Chrome, Opera).
但是,在所有主要的web浏览器(即Firefox、Safari、Chrome、Opera)中,XMLHttpRequest(即AJAX调用)的实现都支持GET、POST、PUT和DELETE。
#2
74
HTML forms support GET and POST. (HTML5 at one point added PUT/DELETE, but those were dropped.)
HTML表单支持GET和POST。(HTML5一度增加了PUT/DELETE,但这些都被删除了。)
XMLHttpRequest supports every method, including CHICKEN, though some method names are matched against case-insensitively (methods are case-sensitive per HTTP) and some method names are not supported at all for security reasons (e.g. CONNECT).
XMLHttpRequest支持每一个方法,包括CHICKEN,尽管有些方法名与大小写敏感地匹配(每个HTTP的方法都是大小写敏感的),有些方法名由于安全原因根本不受支持(例如,CONNECT)。
Browsers are slowly converging on the rules specified by XMLHttpRequest, but as the other comment pointed out there are still some differences.
浏览器正在慢慢地融合XMLHttpRequest所指定的规则,但是正如另一条注释所指出的那样,仍然存在一些差异。
#3
40
XMLHttpRequest
is a standard object in the JavaScript Object model.
XMLHttpRequest是JavaScript对象模型中的一个标准对象。
According to Wikipedia, XMLHttpRequest
first appeared in Internet Explorer 5 as an ActiveX object, but has since been made into a standard and has been included for use in JavaScript in the Mozilla family since 1.0, Apple Safari 1.2, Opera 8.0, and IE 7.0.
根据*,XMLHttpRequest最初是作为ActiveX对象出现在Internet Explorer 5中,但后来成为了一个标准,从1.0、Apple Safari 1.2、Opera 8.0和IE 7.0开始,XMLHttpRequest在Mozilla家族的JavaScript中都有使用。
The open()
method on the object takes the HTTP Method as an argument - and is specified as taking any valid HTTP method (see the item number 5 of the link) - including GET
, POST
, HEAD
, PUT
and DELETE
, as specified by RFC 2616.
对象上的open()方法将HTTP方法作为参数,并指定为使用任何有效的HTTP方法(参见链接的第5项)——包括GET、POST、HEAD、PUT和DELETE,由RFC 2616指定。
作为附加说明IE 7-8只允许以下HTTP方法:“GET”、“POST”、“HEAD”、“PUT”、“DELETE”、“MOVE”、“PROPFIND”、“PROPPATCH”、“MKCOL”、“COPY”、“LOCK”、“UNLOCK”和“OPTIONS”。
#4
15
I believe those comments refer specifically to the browsers, i.e., clicking links and submitting forms, not XMLHttpRequest
. XMLHttpRequest
is just a custom client that you wrote in JavaScript that uses the browser as a runtime.
我认为这些评论是专门针对浏览器的。,单击链接并提交表单,而不是XMLHttpRequest。XMLHttpRequest只是用JavaScript编写的一个定制客户机,使用浏览器作为运行时。
UPDATE: To clarify, I did not mean (though I did write) that you wrote XMLHttpRequest
; I meant that you wrote the code that uses XMLHttpRequest
. The browsers do not natively support XMLHttpRequest
. XMLHttpRequest
comes from the JavaScript runtime, which may be hosted by a browser, although it isn't required to be (see Rhino). That's why people say browsers don't support PUT
and DELETE
—because it's actually JavaScript that is supporting them.
更新:为了澄清这一点,我并不是说(尽管我写过)您编写了XMLHttpRequest;我的意思是您编写了使用XMLHttpRequest的代码。浏览器并没有本地支持XMLHttpRequest。XMLHttpRequest来自JavaScript运行时,该运行时可以由浏览器托管,尽管不需要这样做(参见Rhino)。这就是为什么人们说浏览器不支持PUT和delete——因为支持它们的实际上是JavaScript。
#5
13
_method hidden field workaround
_method隐藏字段的解决方法
Used in Rails and could be adapted to any framework:
适用于Rails,可适用于任何框架:
-
add a hidden
_method
parameter to any form that is not GET or POST:向任何未获取或发布的表单添加隐藏的_method参数:
<input type="hidden" name="_method" value="DELETE">
This can be done automatically in frameworks through the HTML creation helper method (e.g. Rails
form_tag
)这可以通过HTML创建助手方法(例如Rails form_tag)在框架中自动完成。
-
fix the actual form method to POST (
<form method="post"
)将实际的表单方法修改为POST (
-
processes
_method
on the server and do exactly as if that method had been sent instead of the actual POST处理服务器上的_method,并按照发送方法而不是实际的POST的方式执行
Rationale / history of why it is not possible: https://softwareengineering.stackexchange.com/questions/114156/why-there-are-no-put-and-delete-methods-in-html-forms
不可能的理由/历史:https://softwareengineer.stackexchange.com/questions/114156 /why-there-are-no-put- delete-methods-in-html-forms
#6
8
Just to add - Safari 2 and earlier definitely didn't support PUT and DELETE. I get the impression 3 did, but I don't have it around to test anymore. Safari 4 definitely does support PUT and DELETE.
添加- Safari 2和更早的版本绝对不支持PUT和DELETE。我得到的印象是3,但我已经没有它来测试了。Safari 4绝对支持PUT和DELETE。
#7
7
YES, PUT, DELETE, HEAD etc HTTP methods are available in all modern browsers.
是的,PUT、DELETE、HEAD等HTTP方法在所有现代浏览器中都是可用的。
To be compliant with XMLHttpRequest Level 2 browsers must support these methods. To check which browsers support XMLHttpRequest Level 2 I recommend CanIUse:
要符合XMLHttpRequest 2级浏览器必须支持这些方法。为了检查哪些浏览器支持XMLHttpRequest级别2,我推荐使用CanIUse:
= xhr2 http://caniuse.com/壮举
Only Opera Mini is lacking support atm (juli '15), but Opera Mini lacks support for everything. :)
只有Opera Mini缺乏支持atm (juli '15),但是Opera Mini缺乏对所有东西的支持。:)
#1
431
HTML forms (up to HTML version 4 and XHTML 1) only support GET and POST as HTTP request methods. A workaround for this is to tunnel other methods through POST by using a hidden form field which is read by the server and the request dispatched accordingly.
HTML表单(直到HTML版本4和XHTML 1)只支持GET和POST作为HTTP请求方法。解决此问题的方法是通过POST方法通过POST方法来使用隐藏的表单字段,该字段由服务器读取,并相应地发送请求。
However, GET, POST, PUT and DELETE are supported by the implementations of XMLHttpRequest (i.e. AJAX calls) in all the major web browsers (IE, Firefox, Safari, Chrome, Opera).
但是,在所有主要的web浏览器(即Firefox、Safari、Chrome、Opera)中,XMLHttpRequest(即AJAX调用)的实现都支持GET、POST、PUT和DELETE。
#2
74
HTML forms support GET and POST. (HTML5 at one point added PUT/DELETE, but those were dropped.)
HTML表单支持GET和POST。(HTML5一度增加了PUT/DELETE,但这些都被删除了。)
XMLHttpRequest supports every method, including CHICKEN, though some method names are matched against case-insensitively (methods are case-sensitive per HTTP) and some method names are not supported at all for security reasons (e.g. CONNECT).
XMLHttpRequest支持每一个方法,包括CHICKEN,尽管有些方法名与大小写敏感地匹配(每个HTTP的方法都是大小写敏感的),有些方法名由于安全原因根本不受支持(例如,CONNECT)。
Browsers are slowly converging on the rules specified by XMLHttpRequest, but as the other comment pointed out there are still some differences.
浏览器正在慢慢地融合XMLHttpRequest所指定的规则,但是正如另一条注释所指出的那样,仍然存在一些差异。
#3
40
XMLHttpRequest
is a standard object in the JavaScript Object model.
XMLHttpRequest是JavaScript对象模型中的一个标准对象。
According to Wikipedia, XMLHttpRequest
first appeared in Internet Explorer 5 as an ActiveX object, but has since been made into a standard and has been included for use in JavaScript in the Mozilla family since 1.0, Apple Safari 1.2, Opera 8.0, and IE 7.0.
根据*,XMLHttpRequest最初是作为ActiveX对象出现在Internet Explorer 5中,但后来成为了一个标准,从1.0、Apple Safari 1.2、Opera 8.0和IE 7.0开始,XMLHttpRequest在Mozilla家族的JavaScript中都有使用。
The open()
method on the object takes the HTTP Method as an argument - and is specified as taking any valid HTTP method (see the item number 5 of the link) - including GET
, POST
, HEAD
, PUT
and DELETE
, as specified by RFC 2616.
对象上的open()方法将HTTP方法作为参数,并指定为使用任何有效的HTTP方法(参见链接的第5项)——包括GET、POST、HEAD、PUT和DELETE,由RFC 2616指定。
作为附加说明IE 7-8只允许以下HTTP方法:“GET”、“POST”、“HEAD”、“PUT”、“DELETE”、“MOVE”、“PROPFIND”、“PROPPATCH”、“MKCOL”、“COPY”、“LOCK”、“UNLOCK”和“OPTIONS”。
#4
15
I believe those comments refer specifically to the browsers, i.e., clicking links and submitting forms, not XMLHttpRequest
. XMLHttpRequest
is just a custom client that you wrote in JavaScript that uses the browser as a runtime.
我认为这些评论是专门针对浏览器的。,单击链接并提交表单,而不是XMLHttpRequest。XMLHttpRequest只是用JavaScript编写的一个定制客户机,使用浏览器作为运行时。
UPDATE: To clarify, I did not mean (though I did write) that you wrote XMLHttpRequest
; I meant that you wrote the code that uses XMLHttpRequest
. The browsers do not natively support XMLHttpRequest
. XMLHttpRequest
comes from the JavaScript runtime, which may be hosted by a browser, although it isn't required to be (see Rhino). That's why people say browsers don't support PUT
and DELETE
—because it's actually JavaScript that is supporting them.
更新:为了澄清这一点,我并不是说(尽管我写过)您编写了XMLHttpRequest;我的意思是您编写了使用XMLHttpRequest的代码。浏览器并没有本地支持XMLHttpRequest。XMLHttpRequest来自JavaScript运行时,该运行时可以由浏览器托管,尽管不需要这样做(参见Rhino)。这就是为什么人们说浏览器不支持PUT和delete——因为支持它们的实际上是JavaScript。
#5
13
_method hidden field workaround
_method隐藏字段的解决方法
Used in Rails and could be adapted to any framework:
适用于Rails,可适用于任何框架:
-
add a hidden
_method
parameter to any form that is not GET or POST:向任何未获取或发布的表单添加隐藏的_method参数:
<input type="hidden" name="_method" value="DELETE">
This can be done automatically in frameworks through the HTML creation helper method (e.g. Rails
form_tag
)这可以通过HTML创建助手方法(例如Rails form_tag)在框架中自动完成。
-
fix the actual form method to POST (
<form method="post"
)将实际的表单方法修改为POST (
-
processes
_method
on the server and do exactly as if that method had been sent instead of the actual POST处理服务器上的_method,并按照发送方法而不是实际的POST的方式执行
Rationale / history of why it is not possible: https://softwareengineering.stackexchange.com/questions/114156/why-there-are-no-put-and-delete-methods-in-html-forms
不可能的理由/历史:https://softwareengineer.stackexchange.com/questions/114156 /why-there-are-no-put- delete-methods-in-html-forms
#6
8
Just to add - Safari 2 and earlier definitely didn't support PUT and DELETE. I get the impression 3 did, but I don't have it around to test anymore. Safari 4 definitely does support PUT and DELETE.
添加- Safari 2和更早的版本绝对不支持PUT和DELETE。我得到的印象是3,但我已经没有它来测试了。Safari 4绝对支持PUT和DELETE。
#7
7
YES, PUT, DELETE, HEAD etc HTTP methods are available in all modern browsers.
是的,PUT、DELETE、HEAD等HTTP方法在所有现代浏览器中都是可用的。
To be compliant with XMLHttpRequest Level 2 browsers must support these methods. To check which browsers support XMLHttpRequest Level 2 I recommend CanIUse:
要符合XMLHttpRequest 2级浏览器必须支持这些方法。为了检查哪些浏览器支持XMLHttpRequest级别2,我推荐使用CanIUse:
= xhr2 http://caniuse.com/壮举
Only Opera Mini is lacking support atm (juli '15), but Opera Mini lacks support for everything. :)
只有Opera Mini缺乏支持atm (juli '15),但是Opera Mini缺乏对所有东西的支持。:)