Response.ok
åºçº¿
广æ³å¯ç¨
èª 2017å¹´3æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
å®éªæ§: è¿æ¯ä¸é¡¹å®éªæ§ææ¯
å¨å°å
¶ç¨äºç产ä¹åï¼è¯·ä»ç»æ£æ¥æµè§å¨å
¼å®¹æ§è¡¨æ ¼ã
Response æ¥å£çåªè¯»å±æ§ ok å
å«ä¸ä¸ªå¸å°å¼ï¼è¡¨æååºæ¯å¦æåï¼ç¶æç å¨ 200-299 èå´å
ï¼.
è¯æ³
var myOK = response.ok;
å¼
示ä¾
In our Fetch Response example (see Fetch Response live) we create a new Request object using the Request() constructor, passing it a JPG path. We then fetch this request using fetch(), extract a blob from the response using Body.blob, create an object URL out of it using URL.createObjectURL, and display this in an <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;
});
});
è§è
| è§è |
|---|
| Fetch > # ref-for-dom-response-okâ¡ > |