Property | Type | Default value | Required | Description |
url | string | - | True | The URL of the developer's server API |
data | string/object/ArrayBuffer | - | False | Request parameter. |
header | Object | - | False | Sets the request header. Referer cannot be set in the header. The default content-type is application/json |
timeout | number | - | False | Timeout duration in milliseconds. |
method | string | GET | False | HTTP request method. |
dataType | string | json | False | The format of the returned data. |
responseType | string | text | False | The type of the response data. |
success | function | - | False | Callback function for successful API calls. |
fail | function | - | False | Callback function for failed API calls. |
complete | function | - | False | Callback function executed after API call ends (regardless of success or failure). |
Value | Description |
OPTIONS | HTTP OPTIONS request |
GET | HTTP GET request |
HEAD | HTTP HEAD request |
POST | HTTP POST request |
PUT | HTTP PUT request |
DELETE | HTTP DELETE request |
TRACE | HTTP TRACE request |
CONNECT | HTTP CONNECT request |
Value | Description |
json | The returned data is in JSON format and will be parsed once using JSON.parse. |
Others | The returned data will not be parsed using JSON.parse |
Value | Description |
text | The response data is text. |
arraybuffer | The response data is an ArrayBuffer. |
Property | Type | Description |
data | string/Object/Arraybuffer | The data returned by the developer server. |
statusCode | number | The HTTP status code returned by the developer server. |
header | Object | The HTTP response header returned by the developer server. |
cookies | Array.<string> | The cookies returned by the developer server in the format of string array (not supported in mini games). |
Property | Type | Description |
errMsg | String | Error message. |
GET
method data, it will be converted to a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
POST
method data with header['content-type']
as application/json
, it will be serialized into JSON.POST
method data with header['content-type']
as application/x-www-form-urlencoded
, it will be converted into a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
wx.request({url: 'test.php', // Example URL, not a real interface addressdata: {x: '',y: ''},header: {'content-type': 'application/json' // Default value},success(res) {console.log(res.data)}})
This method is called using RequestTask.abort().
This method is called using RequestTask.onChunkReceived(function listener).
Property | Type | Description |
res | Object | The response for each new chunk received from the developer's server. |
Structural property | Type | Description |
data | ArrayBuffer | The returned chunk buffer. |
This method is called using RequestTask.offChunkReceived(function listener).
const listener = function (res) { console.log(res) }RequestTask.onChunkReceived(listener)RequestTask.offChunkReceived(listener) // Must pass the same function object used in onChunkReceived
This method is called using RequestTask.onHeadersReceived(function listener).
Property | Type | Description |
header | Object | The HTTP response header returned by the developer server. |
statusCode | Number | The HTTP status code returned by the developer's server (currently not returned in the developer tools, but can be viewed on a real device) |
cookies | Array.<string> | The cookies returned by the developer server in the format of string array. |
This method is called using RequestTask.offHeadersReceived(function listener).
const listener = function (res) { console.log(res) }RequestTask.onHeadersReceived(listener)RequestTask.offHeadersReceived(listener) // Must pass the same function object used in onHeadersReceived