1. 程式人生 > >第十二週作業 FETCH FETCH 將XMLrequest 改寫成fetch

第十二週作業 FETCH FETCH 將XMLrequest 改寫成fetch

FETCH

詳解Fetch API

相容性

注意:由於Fetch API是基於Promise設計,舊瀏覽器不支援Promise,需要使用pollyfill es6-promise

  1. Fetch使用說明
fetch(url, options).then(function(response) { 
// handle HTTP response
}, function(error) { // handle network error }) 

說明:
a. fetch api返回的是一個promise物件
b.Options:

  • method(String): HTTP請求方法,預設為GET
  • body(String): HTTP的請求引數
  • headers(Object): HTTP的請求頭,預設為{}
  • credentials(String): 預設為omit,忽略的意思,也就是不帶cookie;還有兩個引數,same-origin,意思就是同源請求帶cookie;include,表示無論跨域還是同源請求都會帶cookie

c.第一個then函式裡面處理的是response的格式,這裡的response具體如下:


 
image.png
  • status(number): HTTP返回的狀態碼,範圍在100-599之間
  • statusText(String): 伺服器返回的狀態文字描述,例如Unauthorized,上圖中返回的是Ok
  • ok(Boolean): 如果狀態碼是以2開頭的,則為true
  • headers: HTTP請求返回頭
  • body: 返回體,這裡有處理返回體的一些方法
    • text(): 將返回體處理成字串型別
    • json(): 返回結果和 JSON.parse(responseText)一樣
    • blob(): 返回一個Blob,Blob物件是一個不可更改的類檔案的二進位制資料
    • arrayBuffer()
    • formData()

FETCH

詳解Fetch API

相容性

注意:由於Fetch API是基於Promise設計,舊瀏覽器不支援Promise,需要使用pollyfill es6-promise

  1. Fetch使用說明
fetch(url, options).then(function(response) { 
// handle HTTP response
}, function(error) { // handle network error }) 

說明:
a. fetch api返回的是一個promise物件
b.Options:

  • method(String): HTTP請求方法,預設為GET
  • body(String): HTTP的請求引數
  • headers(Object): HTTP的請求頭,預設為{}
  • credentials(String): 預設為omit,忽略的意思,也就是不帶cookie;還有兩個引數,same-origin,意思就是同源請求帶cookie;include,表示無論跨域還是同源請求都會帶cookie

c.第一個then函式裡面處理的是response的格式,這裡的response具體如下:


  image.png
  • status(number): HTTP返回的狀態碼,範圍在100-599之間
  • statusText(String): 伺服器返回的狀態文字描述,例如Unauthorized,上圖中返回的是Ok
  • ok(Boolean): 如果狀態碼是以2開頭的,則為true
  • headers: HTTP請求返回頭
  • body: 返回體,這裡有處理返回體的一些方法
    • text(): 將返回體處理成字串型別
    • json(): 返回結果和 JSON.parse(responseText)一樣
    • blob(): 返回一個Blob,Blob物件是一個不可更改的類檔案的二進位制資料
    • arrayBuffer()
    • formData()

將XMLrequest 改寫成fetch

 

var header = document.querySelector('header');
var section = document.querySelector('section');

fetch('https://raw.githubusercontent.com/gmhcy/ONEPIECE/master/onepiece.json')

.then(function(response)

return response.json();

})

.then(function(myJson{

var onepiece = myJson;

onepieceHeader(onepiece);
showpeople(onepiece);

});

 

function onepieceHeader(jsonObj){
var headline=document.createElement('h1');
headline.textContent=jsonObj['tou'];
header.appendChild(headline);
var jies=document.createElement('p');
jies.textContent=jsonObj['jieshao'];
header.appendChild(jies);
}
function showpeople(jsonObj){
var heroes=jsonObj['members'];
for (var i = 0; i <heroes.length; i++) {
var myArticle = document.createElement('article');
var myH2 = document.createElement('h2');
var myPara1 = document.createElement('p');
var myPara2 = document.createElement('p');
var myPara3 = document.createElement('p');
var myPara4 = document.createElement('p');
myH2.textContent = heroes[i].name;
myPara1.textContent = 'age: ' + heroes[i].age;
myPara2.textContent = 'power: ' + heroes[i].power;
myPara3.textContent = 'hobby:'+heroes[i].hobby;
myPara4.textContent = 'dream:'+heroes[i].dream;
myArticle.appendChild(myH2);
myArticle.appendChild(myPara1);
myArticle.appendChild(myPara2);
myArticle.appendChild(myPara3);
myArticle.appendChild(myPara4);
section.appendChild(myArticle);
}
}

詳解Fetch API

相容性

注意:由於Fetch API是基於Promise設計,舊瀏覽器不支援Promise,需要使用pollyfill es6-promise

  1. Fetch使用說明
fetch(url, options).then(function(response) { 
// handle HTTP response
}, function(error) { // handle network error }) 

說明:
a. fetch api返回的是一個promise物件
b.Options:

  • method(String): HTTP請求方法,預設為GET
  • body(String): HTTP的請求引數
  • headers(Object): HTTP的請求頭,預設為{}
  • credentials(String): 預設為omit,忽略的意思,也就是不帶cookie;還有兩個引數,same-origin,意思就是同源請求帶cookie;include,表示無論跨域還是同源請求都會帶cookie

c.第一個then函式裡面處理的是response的格式,這裡的response具體如下:


  image.png
  • status(number): HTTP返回的狀態碼,範圍在100-599之間
  • statusText(String): 伺服器返回的狀態文字描述,例如Unauthorized,上圖中返回的是Ok
  • ok(Boolean): 如果狀態碼是以2開頭的,則為true
  • headers: HTTP請求返回頭
  • body: 返回體,這裡有處理返回體的一些方法
    • text(): 將返回體處理成字串型別
    • json(): 返回結果和 JSON.parse(responseText)一樣
    • blob(): 返回一個Blob,Blob物件是一個不可更改的類檔案的二進位制資料
    • arrayBuffer()
    • formData()

詳解Fetch API

相容性

注意:由於Fetch API是基於Promise設計,舊瀏覽器不支援Promise,需要使用pollyfill es6-promise

  1. Fetch使用說明
fetch(url, options).then(function(response) { 
// handle HTTP response
}, function(error) { // handle network error }) 

說明:
a. fetch api返回的是一個promise物件
b.Options:

  • method(String): HTTP請求方法,預設為GET
  • body(String): HTTP的請求引數
  • headers(Object): HTTP的請求頭,預設為{}
  • credentials(String): 預設為omit,忽略的意思,也就是不帶cookie;還有兩個引數,same-origin,意思就是同源請求帶cookie;include,表示無論跨域還是同源請求都會帶cookie

c.第一個then函式裡面處理的是response的格式,這裡的response具體如下:


  image.png
  • status(number): HTTP返回的狀態碼,範圍在100-599之間
  • statusText(String): 伺服器返回的狀態文字描述,例如Unauthorized,上圖中返回的是Ok
  • ok(Boolean): 如果狀態碼是以2開頭的,則為true
  • headers: HTTP請求返回頭
  • body: 返回體,這裡有處理返回體的一些方法
    • text(): 將返回體處理成字串型別
    • json(): 返回結果和 JSON.parse(responseText)一樣
    • blob(): 返回一個Blob,Blob物件是一個不可更改的類檔案的二進位制資料
    • arrayBuffer()
    • formData()

 

var header = document.querySelector('header');
var section = document.querySelector('section');

fetch('https://raw.githubusercontent.com/gmhcy/ONEPIECE/master/onepiece.json')

.then(function(response)

return response.json();

})

.then(function(myJson{

var onepiece = myJson;

onepieceHeader(onepiece);
showpeople(onepiece);

});

 

function onepieceHeader(jsonObj){
var headline=document.createElement('h1');
headline.textContent=jsonObj['tou'];
header.appendChild(headline);
var jies=document.createElement('p');
jies.textContent=jsonObj['jieshao'];
header.appendChild(jies);
}
function showpeople(jsonObj){
var heroes=jsonObj['members'];
for (var i = 0; i <heroes.length; i++) {
var myArticle = document.createElement('article');
var myH2 = document.createElement('h2');
var myPara1 = document.createElement('p');
var myPara2 = document.createElement('p');
var myPara3 = document.createElement('p');
var myPara4 = document.createElement('p');
myH2.textContent = heroes[i].name;
myPara1.textContent = 'age: ' + heroes[i].age;
myPara2.textContent = 'power: ' + heroes[i].power;
myPara3.textContent = 'hobby:'+heroes[i].hobby;
myPara4.textContent = 'dream:'+heroes[i].dream;
myArticle.appendChild(myH2);
myArticle.appendChild(myPara1);
myArticle.appendChild(myPara2);
myArticle.appendChild(myPara3);
myArticle.appendChild(myPara4);
section.appendChild(myArticle);
}
}