WeChat Open Platform

Mini Programs Documentation

Initiating a request

wx.request(OBJECT)

wx.request initiates an HTTPS request.

OBJECT parameter descriptions:

Parameter name Type Required Description
url String Yes Developer server interface address
data Object, String No Request parameters
header Object No Sets request header, referers cannot be set in the header
method String No Default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
dataType String No Default is json. If dataType was set as json, a JSON.parse of the response data will be tried
success Function No Callback function returned if developer server received successfully, res = {data: 'Data returned by developer server'}
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Description
data Data returned by developer server
statusCode Status code returned by developer server

Data descriptions The data that is finally sent to the server is string type. If the data that is imported is not string type, it will be converted into strings. The conversion rules are as follows:

  • JSON serialization will be performed on data where header['content-type'] is 'application/json'.
  • Data where header['content-type'] is 'application/x-www-form-urlencoded' will be converted into query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...).

Sample code:

wx.request({
  url: 'test.php', //example only, not a real interface address
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})

Bugs & Tips

  1. tip: content-type default is 'application/json'
  2. bug: In Developer Tools version 0.10.102800, an exception is set for header content-type.
  3. tip: The client's HTTPS TLS version is 1.2, but some Android models still do not support TLS 1.2. Therefore, please ensure that the HTTPS server's TLS version supports version 1.2 or below.
  4. tip: Method values that need to be noted must be written in capitals (for example: GET).
  5. tip: There cannot be any ports in urls.
  6. tip: The default timeout length and maximum timeout length for requests are both 60s.
  7. tip: The maximum concurrency for requests is 5.
  8. tip: The network referer cannot be set, its format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, wherein {appid} is the Mini Program's appid and {version} is the Mini Program's version number. If the version number is 0 this indicates that it is the development version.