Fetch的用法
在原生JS里已经支持了fetch的AJAX的请求
1.举个例子
fetch('http://example.com/movies.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});
关注两个点
①是fetch请求返回一个Promise对象
②关注这个Fetch的内置方法:json()
2.优雅的写法
async clickAdd(){
try{
const response =await fetch(url)
const data=await response.json()
}catch(err){
console.log(err)
}
}

浙公网安备 33010602011771号