封装一个uniapp的请求
代码:
//创建一个js记得在页面中引用
const baser_url = "http://v.juhe.cn"
export const http = (opts) => { //
return new Promise((resovle,reject)=>{
return uni.request({
url: baser_url + opts.url,
method: opts.methods || 'POST', //
header:{token:uni.getStorageInfoSync("token")},
data: opts.data || {},
}) //
})
}
//页面中使用
//
http({//
url: "/toutiao/index?",//
data: {//
type: "top",//
page_size: 1//
},//
methods: "POST",//
}).then((res) => {//
console.log(JSON.stringify(res))//
}).catch((err) => {//
console.log(JSON.stringify(err))//
})