uniapp发起请求两种方式

1.uniapp.request({})
uni.request({

uni.request({
				url: this.$api+'/Test/student/test',
				header: {
					'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
				},
				//请求成功后返回
				success: (res) => {
					// 请求成功之后将数据给Info
					if(res.statusCode===200)
					{
						self.Info = res.data;
					}
				}
			});

2.this.$axios({})

this.$axios({
	method: 'get',
	url: this.$api + '/Test/student/test'
	// data: {
	// 	userName: 'Lan',
	// 	password: '123'
	// },
})
	.then(function(res) {
		if (res.data.code === 1234) {
			self.Info = res.data
		}
	})
	.catch(function(error) {
		console.log(error.statusCode)
	})

this.a p i , t h i s . api,this.apithis.axios要在main.js当中注册为全局变量

Vue.prototype.$api='http://192.168.2.114:8099'
Vue.prototype.$axios=axios

完整示例代码:

<template>
	<view class="content">
		<span>用户名:</span>
		<input class="username" type="text" :value="username" placeholder="请输入用户名" />
		<view class="">
			<button type="default" @click="getInfo()">测试</button>
			{{ Info }}
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			Info: '',
			username: '工程师',
			pwd: ''
		}
	},
	methods: {
		getInfo() {
			// var self = this;
			// uni.request({
			// 	url: this.$api+'/Test/student/test',
			// 	header: {
			// 		'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
			// 	},
			// 	//请求成功后返回
			// 	success: (res) => {
			// 		// 请求成功之后将数据给Info
			// 		if(res.statusCode===200)
			// 		{
			// 			self.Info = res.data;
			// 		}
			// 	}
			// });
			var self = this
			this.$axios({
				method: 'get',
				url: this.$api + '/Test/student/test'
				// data: {
				// 	userName: 'Lan',
				// 	password: '123'
				// },
			})
				.then(function(res) {
					if (res.data.code === 1234) {
						self.Info = res.data
					}
				})
				.catch(function(error) {
					console.log(error.statusCode)
				})
		}
	}
}
</script>
<style>
.span {
	width: 30px;
}
.username {
	border-radius: 10px;
	border: 2rpx solid #007aff;
	width: 80px;
	padding: 10px;
}
</style>
<!-- <template>
	<p>用户名:</p>
	<input type="text" placeholder="请输入用户名" value=""/>
	
</template>
<script></script>
<style></style> -->

版权声明:本文为weixin_42236617原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。