新建http/http.js文件
//封装get和post请求
function req(method,url, data) {
var urls = "http://www.dadesc.com";
uni.showLoading({
title:'加载中...'
});
var datas = "";
uni.request({
method: method,
url: urls+url,
data: data,
success(res) {
datas = res;
uni.hideLoading()
},
fail(err) {
datas = err;
uni.showToast({
title: '请求失败',
icon: 'none',
duration: 1500,
})
uni.hideLoading()
}
})
return datas;
}
module.exports = {
req
}
main.js引用
import http from './http/http.js'
Vue.prototype.http = http;
项目直接使用
var data = this.http.req("GET",'/index/login',{});
console.log(data)