1.1.9 • Published 2 years ago

@uni/request v1.1.9

Weekly downloads
68
License
BSD-3-Clause
Repository
github
Last release
2 years ago

request

npm

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

PropertyTypeDescriptionRequiredDefault
optionsobject true-
options.urlstring Required,the request urltrue-
options.headersobject The request headersfalse{  'Content-Type': 'application/json'}
options.methodstring Values:GET/POST/PUT/DELETE/PATCH/HEAD/JSONP,mini app only support GET/POST/JSONPfalseGET 
options.dataobject - 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.timeoutnumber timeoutfalse20000 (ms)
options.dataTypestring Set return data type, json or text, If the conversion fails, returns as it isfalsejson 
options.jsonpCallbackstring The callback method name of jsonp, which only takes effect when method is JSONPfalse__uni_jsonp_handler 
options.jsonpCallbackPropstring The name of the callback attribute of jsonp, which only takes effect when the method is JSONPcallback 
options.successFunction The callback function for a successful API callfalse-
options.failFunction The callback function for a failed API callfalse-
options.completeFunction 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)

PropertyTypeDefaultRequiredDescriptionSupported
responseTypestringtextx响应的数据类型
enableHttp2booleanfalsex开启 http2
enableQuicbooleanfalsex开启 quic
enableCachebooleanfalsex开启 cache
cloudCacheobject/booleanfalsex开启云加速

Success return:Response

PropertyTypeDescription
responseobject-
response.datastring 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.headersobject Response headers
response.statusnumber Response status

Non-universal return parameters (due to the destruction of the ability of one code, it is not recommended to use)

PropertyTypeDescriptionSupported
cookiesArray.<string>The cookies returned by the developer server, in the format of a string array
profileObjectSome debugging information during the network request

Fail return:

PropertyTypeDescription
errorobject-
error.codenumber Error code
error.messagestring 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