my.request

Network request of a Mini Program.

Notes

  1. Configure the domain name whitelist at first under Mini Program > Configuration> Server Domain Whitelist. The Mini Program can only communicate with the domain names in the whitelist during the API calls: my.request (send HTTP request), my.uploadFile (upload file), and my.downloadFile (download file).
  2. During the Mini Program development, in the developer tool, select whether to ignore the httpRequest domain name validity check under Details > Domain name information. If yes, the domain name validity will not be checked in the simulator, preview, and real machine debugging scenarios. However, before the Mini Program goes online, you must maintain the domain names in the whitelist, otherwise, the domain names cannot be effective in the official release.

Important: my.request request header is {'content-type': 'application/json'} by default, instead of {'content-type': 'application/x-www-form-urlencoded'}.

Sample Code

copy
my.request({
  url: 'https://httpbin.org/post',
  method: 'POST',
  data: {
    from: 'Mini Program',
    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

Property

Type

Required

Description

url

String

Yes

Target server url.

headers

Object

No

Set the request HTTP header. The default value is 

{'content-type': 'application/json'}

.

method

String

No

The default value is GET. Both GET and POST are supported.

data

Object

No

Request parameter.

timeout

Number

No

Timeout period in ms. The default value is 30000.

dataType

String

No

Expected format of the returned data. The following formats are supported:

  • json
  • text
  • base64

The default format is json.

enableCookie

Boolean

No

Indicates whether the cookie request header is used. Valid values are:

  • true: indicates the cookie request header is used.
  • false: indicates the cookie request header is ignored and any available and non-expired cookies that are returned by the server in the previous response are automatically used.

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 is completed. This function is always executed no matter the call succeeds or fails.

Data Parameter Description

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

  • If the method is GET, the data will be 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 type of the incoming parameter is Object with the following attributes:

Property

Type

Description

data

String

Response data. The format depends on the value of dataType in the request.

status

Number

Response code.

headers

Object

Response header.

Error Code

Error code

Error message

Further action

2

URL parameter cannot be empty

Check whether the URL is in HTTPS format and whether all parameters in URL are correct.

4

Not authorized to call the interface

Tip: This error might result from several reasons. For details, see the Further action column.

Errors occur because the server domain authorization list/whitelist is not configured. In this case, configure the server domain whitelist under Mini Program > Configuration> Server Domain Whitelist. The mini program can only communicate with the domains in the whitelist when calling the my.request and my.uploadFile APIs. If the server domain whitelist is updated, the whitelist comes effective only when a new version of the mini program is published.

Notes:

  • After adding the server domain to the whitelist, must release a new version to make it effective.
  • The server domain to be configured is the domain name in the url parameter.

Errors occur because there is something wrong with your account and you cannot login to the mini program platform. In this case, when you debug in the Mini Program Studio, select Ignore httpRequest domain name validity check or Ignore Webview domain name validity check under Details > Domain name information, and then preview the debugging request.

Errors occur because the request domain name is incorrect. In this case, check whether the request domain name is the same as that is configured in the server domain whitelist.

12

Network error

Seems the connection to the internet is lost. Check whether the network environment is normal and whether the server is stable.

13

Timeout

Check whether the network environment is normal and if the server is responding properly. If the request takes a long time, you can appropriately set the timeout.

14

Decoding failed. JSON parse data error.

  1. Check whether the value of the dataType parameter is correct:
  • If dataType is json: The mini program framework fails to parse the response result by using JSON.parse.
  • If dataType is text: The format of the returned content is incorrect.
  • If dataType is base64: Conversion failed.
  1. It is recommended to check whether the request and response data formats between the frontend and backend are consistent. For example, if the response data format (e.g. text ) does not match the request parameter dataType value (e.g. JSON), it will cause this error. Please modify the backend response data format to JSON.
    Notes:
  • If the backend is PHP or .NET, check whether the response content carries a BOM, and remove the UTF-8 BOM header.
  • If the server needs to return a non-JSON string, set the dataType parameter of my.request to text.

19

HTTP error

The error occurs when the HTTP status code in the response is >= 400. You can try to open the IDE debugger > Network to view detailed error information, or check the statusand data parameter in the call back function about the error.

For other references, see HTTP status code.

20

The request URL does not support HTTP

The operation is cancelled because of URL errors. The request URL does not support HTTP. Use HTTPs in the request URL.

Return Value

RequestTask

Network request task object.

Method

RequestTask.abort()

Note:

If Not authorized to call the interface is returned, configure the domain whitelist under Mini Program > Configuration> Server Domain Whitelist in the mini program platform.