1. 安装fetch
npm install whatwg-fetch -S
2. 页面中使用fetch
// 引入fetch
import 'whatwg-fetch';
fetch('https://api.apiopen.top/getJoke?page=1&count=5&type=video',{
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then(res => { // 第一个then返回的是请求相关参数
return res.json(); // 必须返回相应数据,不然第二个then中无法接收到数据
}).then(json => { // 返回的是数据
console.log(json)
}).then(err => { // 返回的是错误
})