request.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view class="uni-hello-text">
  6. 请点击按钮向服务器发起请求
  7. </view>
  8. <view class="uni-textarea uni-common-mt">
  9. <textarea :value="res"></textarea>
  10. </view>
  11. <view class="uni-btn-v uni-common-mt">
  12. <button type="primary" @click="sendRequest" :loading="loading">发起请求(Callback)</button>
  13. <button type="primary" @click="sendRequest('promise')" :loading="loading">发起请求(Promise)</button>
  14. <button type="primary" @click="sendRequest('await')" :loading="loading">发起请求(Async/Await)</button>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. const requestUrl = 'https://unidemo.dcloud.net.cn/ajax/echo/text?name=uni-app'
  21. const duration = 2000
  22. export default {
  23. data() {
  24. return {
  25. title: 'request',
  26. loading: false,
  27. res: ''
  28. }
  29. },
  30. methods: {
  31. sendRequest(mode) {
  32. this.loading = true;
  33. switch (mode) {
  34. case 'promise':
  35. this._requestPromise();
  36. break;
  37. case 'await':
  38. this._requestAwait();
  39. break;
  40. default:
  41. this._request();
  42. break;
  43. }
  44. },
  45. _request() {
  46. uni.request({
  47. url: requestUrl,
  48. dataType: 'text',
  49. data: {
  50. noncestr: Date.now()
  51. },
  52. success: (res) => {
  53. console.log('request success', res)
  54. uni.showToast({
  55. title: '请求成功',
  56. icon: 'success',
  57. mask: true,
  58. duration: duration
  59. });
  60. this.res = '请求结果 : ' + JSON.stringify(res);
  61. },
  62. fail: (err) => {
  63. console.log('request fail', err);
  64. uni.showModal({
  65. content: err.errMsg,
  66. showCancel: false
  67. });
  68. },
  69. complete: () => {
  70. this.loading = false;
  71. }
  72. });
  73. },
  74. _requestPromise() {
  75. // #ifndef VUE3
  76. uni.request({
  77. url: requestUrl,
  78. dataType: 'text',
  79. data: {
  80. noncestr: Date.now()
  81. }
  82. }).then(res => {
  83. console.log('request success', res[1]);
  84. uni.showToast({
  85. title: '请求成功',
  86. icon: 'success',
  87. mask: true,
  88. duration: duration
  89. });
  90. this.res = '请求结果 : ' + JSON.stringify(res[1]);
  91. this.loading = false;
  92. }).catch(err => {
  93. console.log('request fail', err);
  94. uni.showModal({
  95. content: err.errMsg,
  96. showCancel: false
  97. });
  98. this.loading = false;
  99. });
  100. // #endif
  101. // #ifdef VUE3
  102. uni.request({
  103. url: requestUrl,
  104. dataType: 'text',
  105. data: {
  106. noncestr: Date.now()
  107. }
  108. }).then(res => {
  109. console.log('request success', res);
  110. uni.showToast({
  111. title: '请求成功',
  112. icon: 'success',
  113. mask: true,
  114. duration: duration
  115. });
  116. this.res = '请求结果 : ' + JSON.stringify(res);
  117. this.loading = false;
  118. }).catch(err => {
  119. console.log('request fail', err);
  120. uni.showModal({
  121. content: err.errMsg,
  122. showCancel: false
  123. });
  124. this.loading = false;
  125. });
  126. // #endif
  127. },
  128. async _requestAwait() {
  129. let res, err
  130. // #ifndef VUE3
  131. [err, res] = await uni.request({
  132. url: requestUrl,
  133. dataType: 'text',
  134. data: {
  135. noncestr: Date.now()
  136. }
  137. });
  138. // #endif
  139. // #ifdef VUE3
  140. try {
  141. res = await uni.request({
  142. url: requestUrl,
  143. dataType: 'text',
  144. data: {
  145. noncestr: Date.now()
  146. }
  147. });
  148. } catch(e){
  149. err=e
  150. }
  151. // #endif
  152. if (err) {
  153. console.log('request fail', err);
  154. uni.showModal({
  155. content: err.errMsg,
  156. showCancel: false
  157. });
  158. } else {
  159. console.log('request success', res)
  160. uni.showToast({
  161. title: '请求成功',
  162. icon: 'success',
  163. mask: true,
  164. duration: duration
  165. });
  166. this.res = '请求结果 : ' + JSON.stringify(res);
  167. }
  168. this.loading = false;
  169. }
  170. }
  171. }
  172. </script>