btn.onclick=function(){
fetch(url+"/stats/getUserList",{
method:"POST",
headers:{
'Content-Type': 'application/x-www-form-urlencoded'
},
body:Qs.stringify(data)
}).then(res=>{
console.log(res)
return res.json();
}).then(data=>{
console.log("返回值:",data)
}).catch(err=>{
console.log(err)
})
}
注意:
fetch
返回的是promise
对象。所以fetch().then()
第一个then
里返回的并不是真正的数据。而是一个promise
,所以我们需要通过链式操作第二个then()
来获取真正的数据。fetch
发送参数是通过body
字段来实现的。body
是fetch
第二个参数的必选参数之一。params
的参数如下
method
(String): HTTP请求方法,默认为GET
;body