my.request

Network request of a Mini Program.

Note

  1. Configure the domain name whitelist at first in the Mini Program detailed page of the Mini Program management background > Setting > Development setting > httpRequest interface request domain name whitelist The Mini Program can communicate with the domain names in the whitelist only during the API calls: HTTP request (my.request), upload file (my.uploadFile), download file (my.downloadFile).
  1. During Mini Program development, in the developer tool Details > Domain name information > Ignore httpRequest domain name validity check, select whether to ignore the validity check. If yes, the domain name validity will not be checked in simulator, preview and real machine debugging scenarios. However, before the Mini Program goes online, it is a must to maintain the communication domain name in the whitelist, otherwise it cannot be called in the official release.

Important: my.request request header has the default {'content-type': 'application/json'}, but not {'content-type': 'application/x-www-form-urlencoded'}

Sample Code

copy
my.request({
  url: 'https://httpbin.org/post',
  method: 'POST',
  data: {
    from: 'Paytm',
    production: 'JSAPI',
  },
  dataType: 'json',
  success: function(res) {
    my.alert({content: 'success'});
  },
  fail: function(res) {
    my.alert({content: 'fail'});
  },
  complete: function(res) {
    my.hideLoading();
    my.alert({content: 'complete'});
  }
});

const task = my.request({url: 'https://httpbin.org/post'})
task.abort()

Parameters

NameTypeMandatoryDescription
urlStringYesTarget server url
headersObjectNoSet the request HTTP header, default   {'content-type': 'application/json'}
methodStringNoDefault GET, supporting GET/POST
dataObjectNoRequest parameter
timeoutNumberNoTimeout period, in ms, 30000 by   default
dataTypeStringNoExpected return data format, default   json, supporting json, text and base64
successFunctionNoCallback function upon call success
failFunctionNoCallback function upon call failure
completeFunctionNoCallback function upon call   completion (to be executed upon either call success or failure)

Data Parameter Description:

Data transferred to the server is eventually of the String type. If it is not, the data will be converted into String. Conversion rules:

  • If the method is GET, the data is converted into query string: encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...
  • If the method is POST and the headers['content-type'] is application/json, the data will be JSON serialized.
  • If the method is POST and the headers['content-type'] is application/x-www-form-urlencoded, the data will be converted into query string: encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...

Success Callback Function

The incoming parameter is of the Object type with the following attributes:

NameTypeDescription
dataStringResponse data, format depending on the dataType parameter in the request
statusNumberResponse code
headersObjectResponse header

Error Code

errorDescription
11No right to cross domain
12Network error
13Timeout
14Decoding failure
19HTTP error
20Request stopped/service end traffic   limit

When the incoming parameter dataType value is json, the Mini Program framework firstly do the JSON.prase operation on the return results. If the parsing failed, it returns error 14. When the incoming parameter dataType value is text, if the returned content has a bad format, it returns error 14. To solve the error, firstly check if the dataType setting.

Return Value

RequestTask

Network request task object

Method

RequestTask.abort()

Note: If the request call returns “No right to call the interface”, it is required to add the domain name address (mini program details > setting > developer setting > httpRequest interface request domain name whitelist) in the request of the open platform configuration background