Skip to main content
Version: Next

RequestTask

Requests tasks over the network.

Methods

abort

Aborts requesting tasks.

Reference

() => void

API Support

APIWeChat Mini-ProgramH5React Native
RequestTask.abort✔️

offHeadersReceived

Un-listens on the HTTP Response Header event.

Reference

(callback: (res: CallbackResult) => void) => void
PropertyTypeDescription
callback(res: CallbackResult) => voidThe callback function for the HTTP Response Header event.

API Support

APIWeChat Mini-ProgramH5React Native
RequestTask.offHeadersReceived✔️

onHeadersReceived

Listens on HTTP Response Header event, which will be earlier than the request completion event.

Reference

(callback: (result: OnHeadersReceivedCallbackResult) => void) => void
PropertyTypeDescription
callback(result: OnHeadersReceivedCallbackResult) => voidThe callback function for the HTTP Response Header event.

API Support

APIWeChat Mini-ProgramH5React Native
RequestTask.onHeadersReceived✔️

Parameters

OnHeadersReceivedCallbackResult

PropertyTypeDescription
headerRecord<string, any>HTTP Response Header returned by the developer server

Sample Code

Example 1

const requestTask = Taro.request({
url: 'test.php', // This value for demonstration purposes only is not a real API URL.
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log(res.data)
}
})
requestTask.abort()

Example 2

const requestTask = Taro.request({
url: 'test.php', // This value for demonstration purposes only is not a real API URL.
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json' // Default value
},
success: function (res) {
console.log(res.data)
}
})
requestTask.then(res => {
console.log(res.data)
})
requestTask.abort()

Example 3

const requestTask = Taro.request(params)
const res = await requestTask
requestTask.abort()

API Support

APIWeChat Mini-ProgramH5React Native
RequestTask.abort✔️
RequestTask.offHeadersReceived✔️
RequestTask.onHeadersReceived✔️