fetch是web提供的一个可以获取异步资源的api,目前还没有被所有浏览器支持,它提供的api返回的是Promise对象,所以你在了解这个api前首先得了解Promise的用法
fetch返回的是一个Promise
.then返回的是一个Response对象 要把它转成json格式
需要这么写
window.onload=function(){
fetch("https://api-hmugo-web.itheima.net/api/public/v1//home/swiperdata",{
method:"GET",
})
.then(res=>res.json())
.then(data=>console.log(data))
}
可以这个配置其他选项
<script>
window.onload=function(){
fetch("https://api-hmugo-web.itheima.net/api/public/v1/goods/detail",{
method:"POST",
body:{ }
})
.then(res=>res.json())
.then(data=>console.log(data))
}
</script>