1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
---
title: OPTIONS
slug: Web/HTTP/Methods/OPTIONS
translation_of: Web/HTTP/Methods/OPTIONS
---
<div>{{HTTPSidebar}}</div>
<p><strong>HTTP 的 <code>OPTIONS 方法</code></strong> 用于获取目的资源所支持的通信选项。客户端可以对特定的 URL 使用 OPTIONS 方法,也可以对整站(通过将 URL 设置为“*”)使用该方法。</p>
<table class="properties">
<tbody>
<tr>
<th scope="row">Request has body</th>
<td>No</td>
</tr>
<tr>
<th scope="row">Successful response has body</th>
<td>Yes</td>
</tr>
<tr>
<th scope="row">{{Glossary("Safe")}}</th>
<td>Yes</td>
</tr>
<tr>
<th scope="row">{{Glossary("Idempotent")}}</th>
<td>Yes</td>
</tr>
<tr>
<th scope="row">{{Glossary("Cacheable")}}</th>
<td>No</td>
</tr>
<tr>
<th scope="row">Allowed in HTML forms</th>
<td>No</td>
</tr>
</tbody>
</table>
<h2 id="语法">语法</h2>
<pre class="syntaxbox notranslate">OPTIONS /index.html HTTP/1.1
OPTIONS * HTTP/1.1
</pre>
<h2 id="示例">示例</h2>
<h3 id="检测服务器所支持的请求方法">检测服务器所支持的请求方法</h3>
<p>可以使用 OPTIONS 方法对服务器发起请求,以检测服务器支持哪些 HTTP 方法:</p>
<pre class="notranslate">curl -X OPTIONS http://example.org -i</pre>
<p>响应报文包含一个 {{HTTPHeader("Allow")}} 首部字段,该字段的值表明了服务器支持的所有 HTTP 方法:</p>
<pre class="notranslate">HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Date: Thu, 13 Oct 2016 11:45:00 GMT
Expires: Thu, 20 Oct 2016 11:45:00 GMT
Server: EOS (lax004/2813)
x-ec-custom-error: 1
Content-Length: 0
</pre>
<h3 id="CORS_中的预检请求">CORS 中的预检请求</h3>
<p>在 <a href="/zh-CN/docs/Web/HTTP/Access_control_CORS">CORS</a> 中,可以使用 OPTIONS 方法发起一个预检请求,以检测实际请求是否可以被服务器所接受。预检请求报文中的 {{HTTPHeader("Access-Control-Request-Method")}} 首部字段告知服务器实际请求所使用的 HTTP 方法;{{HTTPHeader("Access-Control-Request-Headers")}} 首部字段告知服务器实际请求所携带的自定义首部字段。服务器基于从预检请求获得的信息来判断,是否接受接下来的实际请求。</p>
<pre class="notranslate">OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type</pre>
<p>服务器所返回的 {{HTTPHeader("Access-Control-Allow-Methods")}} 首部字段将所有允许的请求方法告知客户端。该首部字段与 {{HTTPHeader("Allow")}} 类似,但只能用于涉及到 CORS 的场景中。</p>
<pre class="notranslate">HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 0
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/plain</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Title</th>
</tr>
<tr>
<td>{{RFC("7231", "OPTIONS", "4.3.7")}}</td>
<td>Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("http/methods", "OPTIONS")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>{{HTTPHeader("Allow")}} header</li>
<li><a href="/zh-CN/docs/Web/HTTP/Access_control_CORS">CORS</a></li>
</ul>
|