RequestTask
Requests tasks over the network.
Methods
abort
Aborts requesting tasks.
() => void
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
RequestTask.abort | ✔️ |
offHeadersReceived
Un-listens on the HTTP Response Header event.
(callback: (res: CallbackResult) => void) => void
Property | Type | Description |
---|---|---|
callback | (res: CallbackResult) => void | The callback function for the HTTP Response Header event. |
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
RequestTask.offHeadersReceived | ✔️ |
onHeadersReceived
Listens on HTTP Response Header event, which will be earlier than the request completion event.
(callback: (result: OnHeadersReceivedCallbackResult) => void) => void
Property | Type | Description |
---|---|---|
callback | (result: OnHeadersReceivedCallbackResult) => void | The callback function for the HTTP Response Header event. |
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
RequestTask.onHeadersReceived | ✔️ |
Parameters
OnHeadersReceivedCallbackResult
Property | Type | Description |
---|---|---|
header | Record<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
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
RequestTask.abort | ✔️ | ||
RequestTask.offHeadersReceived | ✔️ | ||
RequestTask.onHeadersReceived | ✔️ |