aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/response/ok/index.html
blob: ef4ef7085d521a8fbb295e516dfaf47de6591787 (plain)
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
---
title: Response.ok
slug: Web/API/Response/ok
translation_of: Web/API/Response/ok
---
<div>{{APIRef("Fetch")}}{{SeeCompatTable}}</div>

<p>{{domxref("Response")}} 接口的只读属性  <code><strong>ok</strong></code> 包含一个布尔值,表明响应是否成功(状态码在200-299范围内).</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var <var>myOK</var> = <var>response</var>.ok;</pre>

<h3 id="值"></h3>

<p> {{domxref("Boolean")}}.</p>

<h2 id="示例">示例</h2>

<p>In our <a href="https://github.com/mdn/fetch-examples/tree/gh-pages/fetch-response">Fetch Response example</a> (see <a href="http://mdn.github.io/fetch-examples/fetch-response/">Fetch Response live</a>) we create a new {{domxref("Request")}} object using the {{domxref("Request.Request","Request()")}} constructor, passing it a JPG path. We then fetch this request using {{domxref("GlobalFetch.fetch","fetch()")}}, extract a blob from the response using {{domxref("Body.blob")}}, create an object URL out of it using {{domxref("URL.createObjectURL")}}, and display this in an {{htmlelement("img")}}.</p>

<p>Note that at the top of the <code>fetch()</code> block we log the response <code>ok</code> value to the console.</p>

<pre class="brush: js">var myImage = document.querySelector('img');

var myRequest = new Request('flowers.jpg');

fetch(myRequest).then(function(response) {
  console.log(response.ok); // returns true if the response returned successfully
  response.blob().then(function(myBlob) {
    var objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
});</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Fetch','#dom-response-ok','ok')}}</td>
   <td>{{Spec2('Fetch')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat("api.Response.ok")}}

<h2 id="相关链接">相关链接</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li>
 <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>