Taro.request(option)
Initiates an HTTPS request. Read related instructions before use.
Data Parameters
The data ultimately sent to the server must be a String. If the passed data is not a String, it will be converted to the String type according to the following rules:
Data from the
GETmethod is converted to a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...).Data from the
POSTmethod withheader['content-type']beingapplication/jsonis serialized into the JSON format.Data from the
POSTmethod withheader['content-type']beingapplication/x-www-form-urlencodedis converted to a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
Type
<T = any, U = any>(option: Option<U>) => RequestTask<T>
Parameters
Option
| Property | Type | Default | Required | Description |
|---|---|---|---|---|
| url | string | Yes | Developer server API URL | |
| data | U | No | Request parameter | |
| dataType | "json" | "other" | No | The returned data format | |
| header | Record<string, any> | No | Sets request Header. Referer is not available in Header.content-type is application/jsonby default. | |
| method | "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | No | HTTP request method | |
| responseType | "text" | "arraybuffer" | No | The response data format | |
| jsonp | boolean | false | No | Specify whether to use jsonp to fetch data on the H5 side |
| jsonpCache | boolean | false | No | Specifies whether data fetched using jsonp should be cached on the H5 side |
| mode | "no-cors" | "cors" | "same-origin" | same-origin | No | Specifies whether cross-domain requests are allowed on the H5 side |
| credentials | "same-origin" | "include" | "omit" | omit | No | Specify whether to carry cookies on the H5 side |
| cache | "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached" | default | No | Specify the cache mode on the H5 side |
| timeout | number | 2000 | No | Specify the request timeout on the H5 side |
| retryTimes | number | 2 | No | Specify the number of request retries on the H5 side |
| backup | string | string[] | No | Specify the backup requestion for requests on the H5 side | |
| dataCheck | () => boolean | No | Specify the data validation function for the request response on the H5 side, and if the result is false, request backup requestion. | |
| useStore | boolean | false | No | Specify whether the request on the H5 side uses caching. |
| storeCheckKey | string | No | Set the key for the request cache checks on the H5 side | |
| storeSign | string | No | Set the request cache signature on the H5 side | |
| storeCheck | () => boolean | No | Set the request checksum function on the H5 side, generally not required | |
| complete | (res: CallbackResult) => void | No | The callback function used when the API call completed (always executed whether the call succeeds or fails) | |
| fail | (res: CallbackResult) => void | No | The callback function for a failed API call | |
| success | (res: CallbackResult) => void | No | The callback function for a successful API call |
SuccessCallbackResult
| Property | Type | Description |
|---|---|---|
| data | T | Data returned by the developer server |
| header | Record<string, any> | HTTP Response Header returned by the developer server |
| statusCode | number | HTTP status code returned by the developer server |
| errMsg | string | Call result |
dataType
Valid values of object.dataType
| Value | Description |
|---|---|
| json | The returned data is in the JSON format. Call JSON.parse on the returned data. |
| Others | Do not call JSON.parse on the returned data. |
method
Valid values of object.method
| Value | Description |
|---|---|
| OPTIONS | HTTP request OPTIONS |
| GET | HTTP request GET |
| HEAD | HTTP request HEAD |
| POST | HTTP request POST |
| PUT | HTTP request PUT |
| DELETE | HTTP request DELETE |
| TRACE | HTTP request TRACE |
| CONNECT | HTTP request CONNECT |
responseType
Valid values of object.responseType
| Value | Description |
|---|---|
| text | The response data is in the text format. |
| arraybuffer | The response data is in the ArrayBuffer format. |
cors_mode
Valid values of CORS mode
| Value | Description |
|---|---|
| no-cors | Cross-domain requests will get an opaque response |
| cors | Allow cross-domain requests |
| same-origin | Requests are always made to the current source |
credentials
Valid values of credentials
| Value | Description |
|---|---|
| include | Always send authentication information such as cookies, HTTP Basic authentication, etc. local to the requested resource domain. |
| same-origin | Authentication information such as cookies, HTTP Basic authentication, etc. is only sent if the URL is the same source as the response script. |
| omit | No cookies are sent. |
Sample Code
Example 1
Taro.request({
url: 'test.php', // This value for demonstration purposes only is not a real API URL.
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // Default value
},
success: function (res) {
console.log(res.data)
}
})
Example 2
const res = await Taro.request(params)
API Support
| API | WeChat Mini-Program | Baidu Smart-Program | Alipay Mini-Program | ByteDance Mini-Program | QQ Mini-Program | H5 | React Native |
|---|---|---|---|---|---|---|---|
| Taro.request | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |