$ npm install universal-request
Used to initiate a network request
$ npm install universal-request --save
import request from 'universal-request';
// How to use it in quickapp
// import chooseImage from 'universal-request/lib/quickapp;
request({
url: 'https://alibaba.github.io/rax/',
method: 'POST',
data: {
from: 'Rax',
},
dataType: 'json'
}).then(response => {})
.catch(error => {});
request(options)
Property | Type | Description | required | Default |
---|---|---|---|---|
options | object |
true | - | |
opthons.url | string |
Required,the request url | true | - |
opthons.headers | object |
The request headers | false | { 'Content-Type': 'application/json' } |
options.method | string |
Values:GET/POST/PUT/DELETE/PATCH/HEAD,mini app only support GET/POST | 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 |
Success return:Promise<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 ,JSONP request has no value |
response.status | number |
Response status,JSONP request has no value |
Fail return:
Property | Type | Description |
---|---|---|
error | object |
- |
error.code | number |
Error code |
error.message | string |
Error message |
error code:
code | message | Description |
---|---|---|
0 | Reason of failure | Request failed except for the listed below |
1 | Request timeout | timeout |
2 | Request not support this platform | not support this platform |
© 2010 - cnpmjs.org x YWFE | Home | YWFE