目前项目中和后端交互基本用axios,axios可以很好的配置请求及响应拦截器,但是有的接口设计是不能返回对应code码,这时候使用全局封装的axios会造成错误拦截。比如导出接口,后端可能返回一串流数据,这时候没有code码 这时axios就会抛出错误。这时候就可以使用fecth方法。
fetch(url,{
method:'get/post',
hearders:{
Content-Type:'application/json',
toke:'用户token值',
……其他hearder配置
},
body:JSON.stringify(data) //data为要传入参数
}).then((res)=>{
//根据后台返回的数据类型进行处理
//数据流 const res = res.blob()
//json const res = res.json()
retrun res
}).then(res=>{
//res是处理后的数据
}).catch(error=>{
// 处理错误
})