Skip to content

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。

    PropertiesTypeDefault valueRequiredDescription
    urlstring-YesDeveloper server interface address
    datastring/object/ArrayBuffer-NoRequest parameters
    headerObject-NoSet the request header. Referer cannot be set in the header. content-type defaults to application/json
    timeoutnumber-NoTimeout in milliseconds
    methodstringGETNoHTTP request method
    dataTypestringjsonNoReturned data format
    responseTypestringtextNoResponse data type
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • Legal values
    ValueDescription
    OPTIONSHTTP request OPTIONS
    GETHTTP request GET
    HEADHTTP request HEAD
    POSTHTTP request POST
    PUTHTTP request PUT
    DELETEHTTP request DELETE
    TRACEHTTP request TRACE
    CONNECTHTTP request CONNECT
    • Legal values
    ValueDescription
    jsonThe returned data is JSON, and the returned data will be JSON.parsed once after returning
    OtherThe returned content will not be JSON.parsed
    • Legal values
    ValueDescription
    textThe response data is text
    arraybufferThe response data is ArrayBuffer
    • Legal values
    ValueDescription
    textThe response data is text
    arraybufferThe response data is ArrayBuffer
  • object.success callback function parameters:

    Parameters: Object res

    PropertiesTypeDescription
    datastring/Object/ArraybufferData returned by the developer server
    statusCodenumberHTTP status code returned by the developer server
    headerObjectHTTP Response Header returned by the developer server
    cookiesArray.<string>Cookies returned by the developer server, in the format of string array
  • object.fail callback function parameter:

    Parameters: Object err

    PropertiesTypeDescription
    errMsgStringError 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:

js
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:

    PropertiesTypeDescription
    resObjectThe Response each time the developer's server returns a new chunk
  • res structure attributes

    PropertiesTypeDescription
    dataArrayBufferReturned 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:

js
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.

    PropertiesTypeDescription
    headerObjectHTTP Response Header returned by the developer server
    statusCodeNumberThe 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)
    cookiesArray.<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:

js
const listener = function (res) { console.log(res) }

RequestTask.onHeadersReceived(listener)
RequestTask.offHeadersReceived(listener) // Pass the same function object used for listening.