微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

uniapp封装get和post请求

新建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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐