request.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. uni.request({
  76. url: requestUrl,
  77. dataType: 'text',
  78. data: {
  79. noncestr: Date.now()
  80. }
  81. }).then(res => {
  82. console.log('request success', res[1]);
  83. uni.showToast({
  84. title: '请求成功',
  85. icon: 'success',
  86. mask: true,
  87. duration: duration
  88. });
  89. this.res = '请求结果 : ' + JSON.stringify(res[1]);
  90. this.loading = false;
  91. }).catch(err => {
  92. console.log('request fail', err);
  93. uni.showModal({
  94. content: err.errMsg,
  95. showCancel: false
  96. });
  97. this.loading = false;
  98. });
  99. },
  100. async _requestAwait() {
  101. const [err, res] = await uni.request({
  102. url: requestUrl,
  103. dataType: 'text',
  104. data: {
  105. noncestr: Date.now()
  106. }
  107. });
  108. if (err) {
  109. console.log('request fail', err);
  110. uni.showModal({
  111. content: err.errMsg,
  112. showCancel: false
  113. });
  114. } else {
  115. console.log('request success', res)
  116. uni.showToast({
  117. title: '请求成功',
  118. icon: 'success',
  119. mask: true,
  120. duration: duration
  121. });
  122. this.res = '请求结果 : ' + JSON.stringify(res);
  123. }
  124. this.loading = false;
  125. }
  126. }
  127. }
  128. </script>