Home >Web Front-end >uni-app >How does uniapp initiate a request?
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】.
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; } } });
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) })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
Complete sample code:
用户名: <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: &#39;get&#39;, url: this.$api + &#39;/Test/student/test&#39; // data: { // userName: &#39;Lan&#39;, // password: &#39;123&#39; // }, }) .then(function(res) { if (res.data.code === 1234) { self.Info = res.data } }) .catch(function(error) { console.log(error.statusCode) }) } } } </script>{{ Info }}
Related free learning recommendations:
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!