Home > Web Front-end > uni-app > body text

How does uniapp initiate a request?

coldplay.xixi
Release: 2023-01-13 00:44:22
Original
7003 people have browsed it

Uniapp initiates a request: 1. Use the [uniapp.request({})] method; 2. Use the [this.$axios({})] method, the code is [this.$axios({ method: 'get',url: this.$api '/Test】.

How does uniapp initiate a request?

The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, thinkpad t480 computer.

Recommended (free): uni-app development tutorial

##uniapp initiates a request Method:

1, use uniapp.request({})method

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;
}
}
});
Copy after login

2, use this.$axios({})Method

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)
})
Copy after login

this.a p i , t h i s . api, this.api, this.axios should be registered as global variables in main.js

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

Complete sample code:


<script>
export default {
data() {
return {
Info: &#39;&#39;,
username: &#39;工程师&#39;,
pwd: &#39;&#39;
}
},
methods: {
getInfo() {
// var self = this;
// uni.request({
// url: this.$api+&#39;/Test/student/test&#39;,
// header: {
// &#39;content-type&#39;: &#39;application/x-www-form-urlencoded&#39; //自定义请求头信息
// },
// //请求成功后返回
// success: (res) => {
// // 请求成功之后将数据给Info
// if(res.statusCode===200)
// {
// self.Info = res.data;
// }
// }
// });
var self = this
this.$axios({
method: &amp;#39;get&amp;#39;,
url: this.$api + &amp;#39;/Test/student/test&amp;#39;
// data: {
// userName: &amp;#39;Lan&amp;#39;,
// password: &amp;#39;123&amp;#39;
// },
})
.then(function(res) {
if (res.data.code === 1234) {
self.Info = res.data
}
})
.catch(function(error) {
console.log(error.statusCode)
})
}
}
}
</script>

Copy after login

Related free learning recommendations:

Programming video

The above is the detailed content of How does uniapp initiate a request?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template