--- title: Response.ok slug: Web/API/Response/ok translation_of: Web/API/Response/ok ---
{{APIRef("Fetch")}}{{SeeCompatTable}}

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

语法

var myOK = response.ok;

 {{domxref("Boolean")}}.

示例

In our Fetch Response example (see Fetch Response live) 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")}}.

Note that at the top of the fetch() block we log the response ok value to the console.

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;
  });
});

规范

Specification Status Comment
{{SpecName('Fetch','#dom-response-ok','ok')}} {{Spec2('Fetch')}} Initial definition

浏览器兼容性

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatChrome(42)}}
{{CompatChrome(41)}}[1]
{{CompatGeckoDesktop(39)}}
34[1]
{{CompatNo}}

29
28[1]

{{CompatNo}}
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] This feature is implemented behind a preference.

相关链接