my.request
Network request of a Mini Program.
Note
- 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
).
- 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
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
Name | Type | Mandatory | Description |
url | String | Yes | Target server url |
headers | Object | No | Set the request HTTP header, default {'content-type': 'application/json'} |
method | String | No | Default GET, supporting GET/POST |
data | Object | No | Request parameter |
timeout | Number | No | Timeout period, in ms, 30000 by default |
dataType | String | No | Expected return data format, default json, supporting json, text and base64 |
success | Function | No | Callback function upon call success |
fail | Function | No | Callback function upon call failure |
complete | Function | No | Callback 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 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 incoming parameter is of the Object type with the following attributes:
Name | Type | Description |
data | String | Response data, format depending on the dataType parameter in the request |
status | Number | Response code |
headers | Object | Response header |
Error Code
error | Description |
11 | No right to cross domain |
12 | Network error |
13 | Timeout |
14 | Decoding failure |
19 | HTTP error |
20 | Request stopped/service end traffic limit |
When the incoming parameter
dataType
value isjson
, the Mini Program framework firstly do theJSON.prase
operation on the return results. If the parsing failed, it returnserror
14. When the incoming parameterdataType
value istext
, if the returned content has a bad format, it returnserror
14. To solve the error, firstly check if thedataType
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