Initiate Request
request
TIP
The API usage is as follows: RequestTask wx.request(Object object)
Functional description: Initiate an HTTPS network request. Please read carefully before useRelated instructions.
Parameters and descriptions: Object object。
Properties Type Default value Required Description url string - Yes Developer server interface address data string/object/ArrayBuffer - No Request parameters header Object - No Set the request header. Referer cannot be set in the header. content-type defaults to application/json timeout number - No Timeout in milliseconds method string GET No HTTP request method dataType string json No Returned data format responseType string text No Response data type success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) - Legal values
Value Description OPTIONS HTTP request OPTIONS GET HTTP request GET HEAD HTTP request HEAD POST HTTP request POST PUT HTTP request PUT DELETE HTTP request DELETE TRACE HTTP request TRACE CONNECT HTTP request CONNECT - Legal values
Value Description json The returned data is JSON, and the returned data will be JSON.parsed once after returning Other The returned content will not be JSON.parsed - Legal values
Value Description text The response data is text arraybuffer The response data is ArrayBuffer - Legal values
Value Description text The response data is text arraybuffer The response data is ArrayBuffer object.success callback function parameters:
Parameters: Object res
Properties Type Description data string/Object/Arraybuffer Data returned by the developer server statusCode number HTTP status code returned by the developer server header Object HTTP Response Header returned by the developer server cookies Array.< string>Cookies returned by the developer server, in the format of string array object.fail callback function parameter:
Parameters: Object err
Properties Type Description errMsg String Error message Return value: RequestTask , request task object.
data parameter description:
The data finally sent to the server is of String type. If the data passed in is not of String type, it will be converted into String. The conversion rules are as follows:
- For data of GET method, the data will be converted into query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
- For data of POST method and header['content-type'] is application/json, the data will be serialized into JSON.
- For data of POST method and header['content-type'] is application/x-www-form-urlencoded, the data will be converted into query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
Sample code:
wx.request({
url: 'test.php', // This is merely an example and not an actual interface address.
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // Default value
},
success(res) {
console.log(res.data)
}
})RequestTask
.abort
TIP
The API usage is as follows: RequestTask.abort()
- Functional description: Interrupt request tasks
.onChunkReceived
TIP
The API usage is as follows: RequestTask.onChunkReceived(function listener)
Functional description: Listen for the Transfer-Encoding Chunk Received event. It is triggered when a new chunk is received.
Parameters and descriptions: function listener,The listener function of the Transfer-Encoding Chunk Received event, the parameter Object res is as follows:
Properties Type Description res Object The Response each time the developer's server returns a new chunk res structure attributes
Properties Type Description data ArrayBuffer Returned chunk buffer
.offChunkReceived
TIP
The API usage is as follows: RequestTask.offChunkReceived(function listener)
This API is supported by mini programs, but not by mini games.
Functional description: Remove the listener function of the Transfer-Encoding Chunk Received event.
Parameters and descriptions: function listener, The listener function passed in by onChunkReceived. If this parameter is not passed, all listeners will be removed.
Sample code:
const listener = function (res) { console.log(res) }
RequestTask.onChunkReceived(listener)
RequestTask.offChunkReceived(listener) // Pass the same function object used for listening..onHeadersReceived
TIP
The API usage is as follows: RequestTask.onHeadersReceived(function listener)
Functional description: Listen to the HTTP Response Header event. It will be earlier than the request completion event.
Parameters and descriptions: function listener,Listener function of the HTTP Response Header event.
Properties Type Description header Object HTTP Response Header returned by the developer server statusCode Number The HTTP status code returned by the developer server (currently the statusCode field will not be returned on the developer tool. You can view this field on the real machine. It will be supported later) cookies Array.< string>Cookies returned by the developer server, in the format of string array
.offHeadersReceived
TIP
The API usage is as follows: RequestTask.offHeadersReceived(function listener)
Functional description: Remove the listener function of the HTTP Response Header event.
Parameters and descriptions: function listener, The listener function passed in by onHeadersReceived. If this parameter is not passed, all listeners will be removed.
Sample code:
const listener = function (res) { console.log(res) }
RequestTask.onHeadersReceived(listener)
RequestTask.offHeadersReceived(listener) // Pass the same function object used for listening.