uniapp怎么发起请求

uniapp发起请求的方法:1、使用【uniapp.request({})】方法;2、使用【this.$axios({})】方法,代码为【this.$axios({method: 'get',url: this.$api+ '/Test】。



本教程操作环境:windows7系统、uni-app2.5.1版本,该方法适用于所有品牌电脑。

推荐(免费):uni-app开发教程

uniapp发起请求的方法:

1、使用uniapp.request({})方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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({})方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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要在main.js当中注册为全局变量

1

2

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

Vue.prototype.$axios=axios

完整示例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74





 

猜你喜欢

转载自blog.csdn.net/zy17822307856/article/details/113026993