request.vue
3.79 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text">
请点击按钮向服务器发起请求
</view>
<view class="uni-textarea uni-common-mt">
<textarea :value="res"></textarea>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @click="sendRequest" :loading="loading">发起请求(Callback)</button>
<button type="primary" @click="sendRequest('promise')" :loading="loading">发起请求(Promise)</button>
<button type="primary" @click="sendRequest('await')" :loading="loading">发起请求(Async/Await)</button>
</view>
</view>
</view>
</template>
<script>
const requestUrl = 'https://unidemo.dcloud.net.cn/ajax/echo/text?name=uni-app'
const duration = 2000
export default {
data() {
return {
title: 'request',
loading: false,
res: ''
}
},
methods: {
sendRequest(mode) {
this.loading = true;
switch (mode) {
case 'promise':
this._requestPromise();
break;
case 'await':
this._requestAwait();
break;
default:
this._request();
break;
}
},
_request() {
uni.request({
url: requestUrl,
dataType: 'text',
data: {
noncestr: Date.now()
},
success: (res) => {
console.log('request success', res)
uni.showToast({
title: '请求成功',
icon: 'success',
mask: true,
duration: duration
});
this.res = '请求结果 : ' + JSON.stringify(res);
},
fail: (err) => {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
},
complete: () => {
this.loading = false;
}
});
},
_requestPromise() {
// #ifndef VUE3
uni.request({
url: requestUrl,
dataType: 'text',
data: {
noncestr: Date.now()
}
}).then(res => {
console.log('request success', res[1]);
uni.showToast({
title: '请求成功',
icon: 'success',
mask: true,
duration: duration
});
this.res = '请求结果 : ' + JSON.stringify(res[1]);
this.loading = false;
}).catch(err => {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
this.loading = false;
});
// #endif
// #ifdef VUE3
uni.request({
url: requestUrl,
dataType: 'text',
data: {
noncestr: Date.now()
}
}).then(res => {
console.log('request success', res);
uni.showToast({
title: '请求成功',
icon: 'success',
mask: true,
duration: duration
});
this.res = '请求结果 : ' + JSON.stringify(res);
this.loading = false;
}).catch(err => {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
this.loading = false;
});
// #endif
},
async _requestAwait() {
let res, err
// #ifndef VUE3
[err, res] = await uni.request({
url: requestUrl,
dataType: 'text',
data: {
noncestr: Date.now()
}
});
// #endif
// #ifdef VUE3
try {
res = await uni.request({
url: requestUrl,
dataType: 'text',
data: {
noncestr: Date.now()
}
});
} catch(e){
err=e
}
// #endif
if (err) {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
} else {
console.log('request success', res)
uni.showToast({
title: '请求成功',
icon: 'success',
mask: true,
duration: duration
});
this.res = '请求结果 : ' + JSON.stringify(res);
}
this.loading = false;
}
}
}
</script>