RequestTask.abort

2020-08-20 15:34 更新

RequestTask.abort

解釋:中斷請(qǐng)求任務(wù)。

方法參數(shù)

無(wú)

示例 

在開(kāi)發(fā)者工具中打開(kāi)


圖片示例



代碼示例

<view class="wrap">
    <view class="card-area">
        <button bind:tap="request" type="primary" loading="{{loading}}" hover-stop-propagation="true">點(diǎn)擊向服務(wù)器發(fā)起請(qǐng)求</button>
        <button bind:tap="abortRequestTask" type="primary" loading="{{loading}}" hover-stop-propagation="true" disabled="{{disabled}}">點(diǎn)擊斷掉前后端鏈接</button>
    </view>
</view>
Page({
    data: {
        disabled: true
    },
    request() {
        const requestTask = swan.request({
            url: 'https://sfc.baidu.com/shopping/nianhuo/bimai',  // 僅為示例,并非真實(shí)的接口地址
            header: {
                'content-type': 'application/json'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: res => {
                swan.showToast({
                    title: '已請(qǐng)求',
                    icon: 'none'
                });
                console.log(res.data);
                this.setData('disabled', false);
            },
            fail: err => {
                console.log('錯(cuò)誤碼:' + err.errCode);
                console.log('錯(cuò)誤信息:' + err.errMsg);
            }
        });
        this.requestTask = requestTask;
    },
    abortRequestTask() {
        this.requestTask.abort();
        swan.showToast({
            title: '已斷掉',
            icon: 'none'
        });
    }
});
:::


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)