my.request
Network request of a Mini Program.
Notes
- 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), andmy.downloadFile
(download file). - 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
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Â
. |
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:
The default format is json. |
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 theheaders['content-type']
isapplication/json
, the data will be JSON serialized. - If the method is
POST
and theheaders['content-type']
isapplication/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 |
status | Number | Response code. |
headers | Object | Response header. |
Error Code
Error | Description |
2 | Incorrect parameter. Check whether the URL is in HTTPS format and all parameters are correct. |
4 | Not authorized to call the interface. The error might result from the following:
|
12 | Network error. |
13 | Timeout. |
14 | Decoding failure. |
19 | HTTP error. |
20 | Request stopped/service end traffic limit. |
Note:
The error code of 14 might be returned in the following cases:
- When the value of
dataType
isjson
, the Mini Program framework firstly perform theJSON.prase
operation on the returned results. If the parsing fails, the error code of 14 is returned. - When the value of
dataType
istext
and the returned content has a bad format, the error code of 14 is returned.
To solve the error, check whether the dataType
setting is incorrect firstly.
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.