RequestTask.abort

解释:中断请求任务。

方法参数

示例

在开发者工具中打开

在开发者工具中打开

在 WEB IDE 中打开

扫码体验

代码示例

百度智能小程序

请使用百度APP扫码

图片示例

RequestTask.abort - 图2

代码示例

  • SWAN
  • JS
  1. <view class="wrap">
  2. <view class="card-area">
  3. <button bind:tap="request" type="primary" loading="{{loading}}" hover-stop-propagation="true">点击向服务器发起请求</button>
  4. <button bind:tap="abortRequestTask" type="primary" loading="{{loading}}" hover-stop-propagation="true" disabled="{{disabled}}">点击断掉前后端链接</button>
  5. </view>
  6. </view>
  1. Page({
  2. data: {
  3. disabled: true
  4. },
  5. request() {
  6. const requestTask = swan.request({
  7. // 仅为示例,并非真实的接口地址
  8. url: 'https://sfc.baidu.com/shopping/nianhuo/bimai',
  9. header: {
  10. 'content-type': 'application/json'
  11. },
  12. method: 'POST',
  13. dataType: 'json',
  14. responseType: 'text',
  15. success: res => {
  16. swan.showToast({
  17. title: '已请求',
  18. icon: 'none'
  19. });
  20. console.log(res.data);
  21. this.setData('disabled', false);
  22. },
  23. fail: err => {
  24. console.log('错误码:' + err.errCode);
  25. console.log('错误信息:' + err.errMsg);
  26. }
  27. });
  28. this.requestTask = requestTask;
  29. },
  30. abortRequestTask() {
  31. this.requestTask.abort();
  32. swan.showToast({
  33. title: '已断掉',
  34. icon: 'none'
  35. });
  36. }
  37. });