在fetch中第一个为请求地址,第二个可以设置请求类型POST,GET,DELETE,UPDATE,PATCH和PUT,随后可以使用then来接收参数,因为异步操作第一个then标明请求类型,第二个then中可以拿到正确的返回值,catch显示返回错误信息。
fetch('https://XXX', {
method: 'POST',
body: JSON.stringify({
username: 'admin',
password: '*****'
}),
headers: { 'Content-Type': 'application/json;charset=utf-8' }
}).then(response => response.json()).then(data => console.log(data)).catch(err => console.log(err));
fetch("https://api.github.com/users")
.then((res) => { return res.json(); console.log(res) })
.then(data => {
console.log(data);
})
.catch(err => console.log(err));