新建http/http.js文件
//封装get和post请求
function req(method,url, data) {
var urls = "http://www.dadesc.com";
const name = uni.getStorageSync('name');
const toker = uni.getStorageSync('toker');
uni.showLoading({
title:'加载中...'
});
return new Promise((resolve, reject) => {
uni.request({
method: method,
url: urls+url+"?name="+name+"&toker="+toker,
data: data,
async: false,
success(res) {
resolve(res)
uni.hideLoading()
},
fail(err) {
reject(err);
uni.showToast({
title: '请求失败',
icon: 'none',
duration: 1500,
})
uni.hideLoading()
}
})
})
}
module.exports = {
req
}
main.js引用
import http from './http/http.js'
Vue.prototype.http = http;
项目直接使用
var data = {"user":this.user,"pwd":this.pwd};
this.http.req("POST",'/index/login',data).then(v=>{
console.log(v)
});