# RequestTask wx.request(Object object)

Initiates an HTTPS request. Read related instructions before use.

# Parameters

# Object object

Property Type Default Value Required Description Minimum Version
url string Yes Developer server API URL
data string/object/ArrayBuffer No Request parameter
header Object No Sets request Header. Referer is not available in Header.
content-type is application/json by default.
method string GET No HTTP request method
dataType string json No The returned data format
responseType string text No The response data format 1.7.0
success function No The callback function for a successful API call
fail function No The callback function for a failed API call
complete function No The callback function used when the API call completed (always executed whether the call succeeds or fails)

Valid values of object.method

Value Description Minimum Version
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

Valid values of object.dataType

Value Description Minimum Version
json The returned data is in the JSON format. Call JSON.parse on the returned data.
Others Do not call JSON.parse on the returned data.

Valid values of object.responseType

Value Description Minimum Version
text The response data is in the text format.
arraybuffer The response data is in the ArrayBuffer format.

# object.success callback function

# Parameters
# Object res
Property Type Description Minimum Version
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 1.2.0

# Return Values

# RequestTask

Start from base library version 1.4.0. Please remaining backward compatible.

Request task object

# Data Parameters

The data ultimately sent to the server must be a String. If the passed data is not a String, it will be converted to the String type according to the following rules:

  • Data from the GET method is converted to a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...).
  • Data from the POST method with header['content-type'] being application/json is serialized into the JSON format.
  • Data from the POST method with header['content-type'] being application/x-www-form-urlencoded is converted to a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...).

# Sample Code

wx.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 (res) {
    console.log(res.data)
  }
})
点击咨询小助手