新建request.js
// uni-app请求封装
export default class Request {
http (router,data={},method) {
// 基础地址
let path = 'http://tm.jiangzi.com/v1';
// 返回promise
return new Promise((resolve,reject) => {
// 请求
uni.request({
url: `${path}${router}`,
data: data,
method:method,
header: {
'content-type': 'application/json' ,
'X-Requested-With':'XMLHttpRequest'
},
success: (res) => {
// 将结果抛出
resolve(res.data)
}
})
})
}
}
新建api.js
import Request from './request.js';
let request = new Request().http
//请求方法
export default {
getPageData: function (data) {
return request('/category',data,'POST')
},
getSearch: function (data) {
return request('/search',data,'POST')
},
getrichtext: function (url) {
return request(url,'','GET')
}
}
组件调用
import api from '../../static/js/api.js';
api.getPageData('').then(res => {
console.log(res)
})