ajax 大多数 Web 浏览器都提供 PUT、DELETE、HEAD 等方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/165779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:44:18  来源:igfitidea点击:

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

httpcross-browserbrowserajax

提问by John Millikin

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,来自其他一些来源,例如:

However, a few quick tests in Firefox show that sending PUTand DELETErequests works as expected -- the XMLHttpRequestcompletes 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 中的一些快速测试表明发送PUTDELETE请求按预期工作——XMLHttpRequest成功完成,请求以正确的方法显示在服务器日志中。是否有我遗漏的某些方面,例如跨浏览器兼容性或非明显限制?

采纳答案by Matthew Murdoch

No. The HTML 5 specmentions:

。HTML 5 规范提到:

The method and formmethod content attributes are enumerated attributes with the following keywords and states:

The keyword get, mapping to the state GET, indicating the HTTP GET method. The GET method should only request and retrieve data and should have no other effect.

The keyword post, mapping to the state POST, indicating the HTTP POST method. The POST method requests that the server accept the submitted form's data to be processed, which may result in an item being added to a database, the creation of a new web page resource, the updating of the existing page, or all of the mentioned outcomes.

The keyword dialog, mapping to the state dialog, indicating that submitting the form is intended to close the dialog box in which the form finds itself, if any, and otherwise not submit.

The invalid value default for these attributes is the GET state

method 和 formmethod 内容属性是具有以下关键字和状态的枚举属性:

关键字get,映射到状态 GET,表示 HTTP GET 方法。GET 方法应仅请求和检索数据,不应有其他影响。

关键字post,映射到状态 POST,表示 HTTP POST 方法。POST 方法请求服务器接受要处理的提交表单的数据,这可能会导致将项目添加到数据库、创建新的网页资源、更新现有页面或所有提到的结果.

关键字dialog,映射到状态对话框,表示提交表单是为了关闭表单所在的对话框(如果有),否则不提交。

这些属性的无效值默认为 GET 状态

I.e. HTML forms only support GETand POSTas 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 表单仅支持GETPOST作为 HTTP 请求方法。一种解决方法是通过使用隐藏的表单字段通过 POST 隧道传输其他方法,该字段由服务器读取并相应地分派请求。

However, GET, POST, PUTand DELETEaresupported by the implementations of XMLHttpRequest (i.e. AJAX calls) in all the major web browsers (IE, Firefox, Safari, Chrome, Opera).

但是,所有主要 Web 浏览器(IE、Firefox、Safari、Chrome、Opera)中的 XMLHttpRequest 实现(即 AJAX 调用)支持GETPOSTPUTDELETE

回答by Anne

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 指定的规则,但正如另一条评论指出的那样,仍然存在一些差异。

回答by Vihung

XMLHttpRequestis a standard object in the JavaScript Object model.

XMLHttpRequest是 JavaScript 对象模型中的标准对象。

According to Wikipedia, XMLHttpRequestfirst 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 7.60-p1, and IE 7.0.

根据维基百科,它XMLHttpRequest首先作为 ActiveX 对象出现在 Internet Explorer 5 中,但此后已成为标准,并自 1.0、Apple Safari 1.2、Opera 7.60-p1 和 IE 7.0 起被包含在 Mozilla 家族的 JavaScript 中使用.

The open()methodon 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, PUTand DELETE, as specified by RFC 2616.

open()方法在物体上发生的HTTP方法作为参数-和被指定为采取任何有效的HTTP方法(参见链路的项目号5) -包括GETPOSTHEADPUTDELETE,作为由RFC 2616中指定

As a side note IE 7–8 only permit the following HTTP methods: "GET", "POST", "HEAD", "PUT", "DELETE", "MOVE", "PROPFIND", "PROPPATCH", "MKCOL", "COPY", "LOCK", "UNLOCK", and "OPTIONS".

作为旁注,IE 7–8 只允许以下 HTTP 方法:“GET”、“POST”、“HEAD”、“PUT”、“DELETE”、“MOVE”、“PROPFIND”、“PROPPATCH”、“MKCOL” 、“复制”、“锁定”、“解锁”和“选项”

回答by Hank Gay

I believe those comments refer specifically to the browsers, i.e., clicking links and submitting forms, not XMLHttpRequest. XMLHttpRequestis just a custom client that youwrote in JavaScript that uses the browser as a runtime.

我相信这些评论专门针对浏览器,即单击链接和提交表单,而不是XMLHttpRequest. XMLHttpRequest只是用 JavaScript 编写的一个自定义客户端,它使用浏览器作为运行时。

UPDATE: To clarify, I did not mean (though I did write) that youwrote XMLHttpRequest; I meant that you wrote the code that uses XMLHttpRequest. The browsers do not natively support XMLHttpRequest. XMLHttpRequestcomes 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 PUTand DELETE—because it's actually JavaScript that is supporting them.

更新:澄清一下,我并不是说(虽然我确实写了)写的XMLHttpRequest;我的意思是你写的代码使用XMLHttpRequest. 浏览器本身不支持XMLHttpRequest. XMLHttpRequest来自 JavaScript 运行时,它可能由浏览器托管,尽管它不是必需的(请参阅Rhino)。这就是为什么人们说浏览器不支持PUT——DELETE因为实际上是 JavaScript 支持它们。

回答by Stijn de Witt

YES, PUT, DELETE, HEAD etc HTTP methods are available in all modern browsers.

YES、PUT、DELETE、HEAD 等 HTTP 方法在所有现代浏览器中都可用。

To be compliant with XMLHttpRequest Level 2browsers mustsupport these methods. To check which browsers support XMLHttpRequest Level 2 I recommend CanIUse:

要符合XMLHttpRequest 级别 2,浏览器必须支持这些方法。要检查哪些浏览器支持 XMLHttpRequest Level 2,我推荐 CanIUse:

http://caniuse.com/#feat=xhr2

http://caniuse.com/#feat=xhr2

Only Opera Mini is lacking support atm (juli '15), but Opera Mini lacks support for everything. :)

只有 Opera Mini 缺乏对 atm 的支持(15 年 7 月),但 Opera Mini 缺乏对所有内容的支持。:)

回答by jharlap

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。