从浏览器中执行HTTP PUT

时间:2022-06-15 18:04:24

I would like to know what the definitive (?) answer is for how to do things other then POST/GET from a browser - either a HTML form or Ajax, as I hear mixed reports on what browsers allow what (specifically on the ajax side).

我想知道最终的(?)答案是如何做其他的事情,然后从浏览器发布/获取——无论是HTML表单还是Ajax,因为我听到了关于浏览器允许什么(特别是在Ajax方面)的混合报告。

When building a back end in RESTful style it is nice to use proper verbs like PUT, HEAD, OPTIONS etc... in rails, a hidden form field called method (IIRC?) is used to simulate this, and at the back end the dispatch to the appropriate controller for the verb. Is this now (in late 2009) necessary? what are the conventions?

在构建RESTful风格的后端时,最好使用正确的动词,如PUT、HEAD、OPTIONS等。在rails中,一个名为method (IIRC?)的隐藏表单字段被用来模拟这个,而在后台,分派给谓词的适当控制器。现在(2009年末)有必要这样做吗?约定是什么?

6 个解决方案

#1


32  

It seems that most browsers don't support other methods besides GET and POST since it is a limitation of HTML forms. Here's another question on the topic:

似乎大多数浏览器除了GET和POST之外不支持其他方法,因为它是HTML表单的一个限制。关于这个话题还有一个问题:

Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

在大多数web浏览器中是否有PUT、DELETE、HEAD等方法?

In order to simulate the PUT, DELETE, etc. methods, you could add a hidden input to a regular GET/POST form with the pseudo-method and have your application translate it so that your controllers see it as if it were a true PUT request, as you mentioned. I've seen this method used in google sitebricks (in java - sorry I don't have any rails-specific reference, but this might at least give you an idea) in this code. I think this is probably the method we are stuck with until something in HTML spec changes (and the browsers with it)

为了模拟PUT、DELETE等方法,您可以使用伪方法向常规GET/POST表单添加一个隐藏的输入,并让应用程序对其进行翻译,以便控制器将其视为一个真正的PUT请求,正如您所提到的。我在谷歌sitebricks(在java中——抱歉,我没有任何特定于rails的引用,但这至少可以让您了解)中使用的方法。我认为这可能是我们一直坚持的方法,直到HTML规范的变化(以及浏览器)

However, GET, POST, PUT and DELETE are supported in AJAX by the major browsers, so there should be no need for a hidden input if you aren't relying on the HTML form.

但是,主要浏览器支持GET、POST、PUT和DELETE,所以如果不依赖HTML表单,就不需要隐藏输入。

#2


6  

You have to use AJAX to do anything other than GETs and POSTs, I would recommend the jQuery Forms plugin to allow you to submit a form as a PUT.

除了获取和发布之外,您必须使用AJAX做任何事情,我建议您使用jQuery Forms插件来提交表单作为PUT。

#3


5  

HTTP has 4 GET,POST,PUT,UPDATE. But most browser support only GET and POST. PUT and UPDATE are simulated by sending additional parameters in request. In rails it's _method="PUT" or _method="UPDATE".

HTTP有4个GET、POST、PUT更新。但是大多数浏览器只支持GET和POST。通过在请求中发送附加参数来模拟PUT和UPDATE。在rails中,它是_method="PUT"或_method="UPDATE"。

#4


3  

I believe the preferred solution to this problem is to use the X-HTTP-Method-Override header. If you search on this term you should find plenty of examples of how to use it.

我认为这个问题的首选解决方案是使用X-HTTP-Method-Override头。如果你搜索这个术语,你应该会发现很多如何使用它的例子。

#5


1  

I think you'll find many firewalls block some of the cooler HTTP verbs. So while it may work for you, if you're trying to create something for the general public consumed from corporate sites, you'll probably want to stick with the basics.

我认为您会发现许多防火墙阻止了一些较冷的HTTP动词。因此,尽管它可能对你有用,但如果你想为从公司网站上消费的公众创建一些东西,你可能想要坚持基本原则。

#6


1  

Besides ajax generated requests, another way of getting these additional methods is with a webdav client. filesystem clients exist for all major operationg systems, and there are some additional clients that can support it for web authoring.

除了ajax生成的请求之外,获取这些附加方法的另一种方法是使用webdav客户机。所有主要操作系统都有文件系统客户端,还有一些其他客户端可以支持它进行web创作。

For instance, the Amaya web browser allows you to edit documents on the web and save them directly to the server, using the PUT method. There are plugins for this on other browsers and several web graphical editors, such as Dreamweaver also support WebDAV.

例如,Amaya web浏览器允许您在web上编辑文档,并使用PUT方法将它们直接保存到服务器。在其他浏览器上也有这样的插件,还有一些web图形编辑器,比如Dreamweaver也支持WebDAV。

WebDAV also supports a number of other methods besides the methods defined in HTTP1.1 for its own use.

WebDAV还支持许多其他方法,除了HTTP1.1中定义的用于自己使用的方法。

#1


32  

It seems that most browsers don't support other methods besides GET and POST since it is a limitation of HTML forms. Here's another question on the topic:

似乎大多数浏览器除了GET和POST之外不支持其他方法,因为它是HTML表单的一个限制。关于这个话题还有一个问题:

Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

在大多数web浏览器中是否有PUT、DELETE、HEAD等方法?

In order to simulate the PUT, DELETE, etc. methods, you could add a hidden input to a regular GET/POST form with the pseudo-method and have your application translate it so that your controllers see it as if it were a true PUT request, as you mentioned. I've seen this method used in google sitebricks (in java - sorry I don't have any rails-specific reference, but this might at least give you an idea) in this code. I think this is probably the method we are stuck with until something in HTML spec changes (and the browsers with it)

为了模拟PUT、DELETE等方法,您可以使用伪方法向常规GET/POST表单添加一个隐藏的输入,并让应用程序对其进行翻译,以便控制器将其视为一个真正的PUT请求,正如您所提到的。我在谷歌sitebricks(在java中——抱歉,我没有任何特定于rails的引用,但这至少可以让您了解)中使用的方法。我认为这可能是我们一直坚持的方法,直到HTML规范的变化(以及浏览器)

However, GET, POST, PUT and DELETE are supported in AJAX by the major browsers, so there should be no need for a hidden input if you aren't relying on the HTML form.

但是,主要浏览器支持GET、POST、PUT和DELETE,所以如果不依赖HTML表单,就不需要隐藏输入。

#2


6  

You have to use AJAX to do anything other than GETs and POSTs, I would recommend the jQuery Forms plugin to allow you to submit a form as a PUT.

除了获取和发布之外,您必须使用AJAX做任何事情,我建议您使用jQuery Forms插件来提交表单作为PUT。

#3


5  

HTTP has 4 GET,POST,PUT,UPDATE. But most browser support only GET and POST. PUT and UPDATE are simulated by sending additional parameters in request. In rails it's _method="PUT" or _method="UPDATE".

HTTP有4个GET、POST、PUT更新。但是大多数浏览器只支持GET和POST。通过在请求中发送附加参数来模拟PUT和UPDATE。在rails中,它是_method="PUT"或_method="UPDATE"。

#4


3  

I believe the preferred solution to this problem is to use the X-HTTP-Method-Override header. If you search on this term you should find plenty of examples of how to use it.

我认为这个问题的首选解决方案是使用X-HTTP-Method-Override头。如果你搜索这个术语,你应该会发现很多如何使用它的例子。

#5


1  

I think you'll find many firewalls block some of the cooler HTTP verbs. So while it may work for you, if you're trying to create something for the general public consumed from corporate sites, you'll probably want to stick with the basics.

我认为您会发现许多防火墙阻止了一些较冷的HTTP动词。因此,尽管它可能对你有用,但如果你想为从公司网站上消费的公众创建一些东西,你可能想要坚持基本原则。

#6


1  

Besides ajax generated requests, another way of getting these additional methods is with a webdav client. filesystem clients exist for all major operationg systems, and there are some additional clients that can support it for web authoring.

除了ajax生成的请求之外,获取这些附加方法的另一种方法是使用webdav客户机。所有主要操作系统都有文件系统客户端,还有一些其他客户端可以支持它进行web创作。

For instance, the Amaya web browser allows you to edit documents on the web and save them directly to the server, using the PUT method. There are plugins for this on other browsers and several web graphical editors, such as Dreamweaver also support WebDAV.

例如,Amaya web浏览器允许您在web上编辑文档,并使用PUT方法将它们直接保存到服务器。在其他浏览器上也有这样的插件,还有一些web图形编辑器,比如Dreamweaver也支持WebDAV。

WebDAV also supports a number of other methods besides the methods defined in HTTP1.1 for its own use.

WebDAV还支持许多其他方法,除了HTTP1.1中定义的用于自己使用的方法。