@uni/request
v1.1.9
Published
[![npm](https://img.shields.io/npm/v/@uni/request.svg)](https://www.npmjs.com/package/@uni/request)
Downloads
11
Readme
request
Used to initiate a network request Note: This API does not support promise calls
Supported
Install
$ npm install @uni/request --save
or
$ npm install @uni/apis --save
Usage
import { request } from '@uni/apis';
request({
url: 'https://alibaba.github.io/rax/',
method: 'POST',
data: {
from: 'Rax',
},
dataType: 'json',
success: (res) => {
console.log('success', res);
},
fail: (res) => {
console.log('fail', res);
},
complete: (res) => {
console.log('complete', res);
}
});
request({
url: 'http://suggest.taobao.com/sug',
method: 'JSONP',
data: {
code: 'utf-8',
q: '卫衣'
},
jsonpCallback: 'cb',
jsonpCallbackProp: 'callback',
timeout: 5000,
success: (res) => {
console.log('success', res);
},
fail: (res) => {
console.log('fail', res);
},
complete: (res) => {
console.log('complete', res);
}
});
You can also import from the big package:
import { request } from '@uni/apis';
Arguments
| Property | Type | Description | Required | Default |
| --- | --- | --- | --- | --- |
| options | object
| | true | - |
| options.url | string
| Required,the request url | true | - |
| options.headers | object
| The request headers | false | { 'Content-Type': 'application/json'} |
| options.method | string
| Values:GET/POST/PUT/DELETE/PATCH/HEAD/JSONP,mini app only support GET/POST/JSONP | false | GET
|
| options.data | object
| - GET or POST set headers['content-Type'] is equal to application/x-www-form-urlencoded
data will apply to URL- In other cases, the data will be converted to a JSON string as request body to the server. | false | - |
| options.timeout | number
| timeout | false| 20000 (ms) |
| options.dataType | string
| Set return data type, json
or text
, If the conversion fails, returns as it is | false | json
|
| options.jsonpCallback | string
| The callback method name of jsonp, which only takes effect when method is JSONP | false | __uni_jsonp_handler
|
| options.jsonpCallbackProp | string
| The name of the callback attribute of jsonp, which only takes effect when the method is JSONP | 否 | callback
|
| options.success | Function
| The callback function for a successful API call | false | - |
| options.fail | Function
| The callback function for a failed API call | false | - |
| options.complete | Function
| The callback function used when the API call completed (always executed whether the call succeeds or fails) | 否 | - |
Non-universal parameters (due to destroying the ability of one code and multiple terminals, it is not recommended to use)
| Property | Type | Default | Required | Description | Supported |
| ------ | -------- | ------ | ---- | ----- | ------- |
| responseType | string
| text | x | 响应的数据类型 | |
| enableHttp2 | boolean
| false | x | 开启 http2 | |
| enableQuic | boolean
| false | x | 开启 quic | |
| enableCache | boolean
| false | x | 开启 cache | |
| cloudCache | object/boolean
| false | x | 开启云加速 | |
Success return:Response
| Property | Type | Description |
| --- | --- | --- |
| response | object
| - |
| response.data | string
| The data returned by the request is converted according to the type declared in the dataType. If the conversion fails, it is returned as it is |
| response.headers | object
| Response headers |
| response.status | number
| Response status |
Non-universal return parameters (due to the destruction of the ability of one code, it is not recommended to use)
| Property | Type | Description | Supported |
| ------ | -------- | ----- | ------- |
| cookies | Array.<string>
| The cookies returned by the developer server, in the format of a string array | |
| profile | Object
| Some debugging information during the network request | |
Fail return:
| Property | Type | Description |
| --- | --- | --- |
| error | object
| - |
| error.code | number
| Error code |
| error.message | string
| Error message |
RequestTask
Request task object
Function
RequestTask.abort() Interrupt request task
Not a universal method (due to the destruction of the ability of one code, it is not recommended to use)
Only WeChat supports the following methods RequestTask.onHeadersReceived(function callback) Listen to the HTTP Response Header event. Will be completed earlier than the requested completion event
RequestTask.offHeadersReceived(function callback) Cancel listening for HTTP Response Header event