描述:利用fetch發(fā)起一個(gè)ajax請(qǐng)求,請(qǐng)求api返回的用戶信息,response中的body并沒有我所需要的json信息,查看資料,fetch()請(qǐng)求獲取的內(nèi)容是一個(gè) Stream 對(duì)象,這個(gè)Stream對(duì)象如何解析,最終拿到body中的json信息var url='http://api.com/getUserInfo';
fetch(url,{ method:'GET', mode:'cors',// 避免cors攻擊
credentials: 'include'
}).then(function(response) { //打印返回的json數(shù)據(jù)
//console.log(response) //狀態(tài)信息
//console.log(response.json()) //一個(gè)promise對(duì)象
//console.log(response.json().data) //報(bào)錯(cuò)了
//如何打印出body中的json信息
response.json().then(function(data){ console.log(data);
});
}).catch(function(e) { console.log("Oops, error");
});繼續(xù)then,就可以打印出數(shù)據(jù)response.json().then(function(data){
console.log(data);
});
如何解析利用fetch請(qǐng)求的數(shù)據(jù)
湖上湖
2018-10-10 17:41:28