# uni.request(param)

发起网络请求。

# 参数

名称 类型 必填 默认值 描述
param RequestOptions<T> - 网络请求参数
名称 类型 必备 默认值 描述
url string - 开发者服务器接口地址
data any | null null 请求的参数 UTSJSONObject|string类型
header UTSJSONObject | null null 设置请求的 header,header 中不能设置 Referer
method "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" "GET" 请求方法
- GET GET方法请求一个指定资源的表示形式,使用 GET 的请求应该只被用于获取数据。
- POST POST方法用于将实体提交到指定的资源,通常导致在服务器上的状态变化或副作用。
- PUT PUT方法用有效载荷请求替换目标资源的所有当前表示。
- PATCH PATCH方法用于对资源应用部分修改。
- DELETE DELETE方法删除指定的资源。
- HEAD HEAD方法请求一个与GET请求的响应相同的响应,但没有响应体。
- OPTIONS OPTIONS 方法用于描述目标资源的通信选项。
timeout number | null 60000 超时时间,单位 ms
withCredentials boolean | null - 跨域请求时是否携带凭证(cookies)
firstIpv4 boolean | null false DNS解析时优先使用ipv4
success (option: RequestSuccess<T>) => void | null null 网络请求成功回调。
fail (option: RequestFail) => void | null null 网络请求失败回调。
complete (option: any) => void | null null 网络请求完成回调,成功或者失败都会调用。
# RequestSuccess<T> 的属性值
名称 类型 必备 默认值 描述
data T | null - 开发者服务器返回的数据
statusCode number - 开发者服务器返回的 HTTP 状态码
header any - 开发者服务器返回的 HTTP Response Header
cookies Array<string> - 开发者服务器返回的 cookies,格式为字符串数组
# RequestFail 的属性值
名称 类型 必备 默认值 描述
errCode 5 | 1000 | 100001 | 100002 | 600003 | 600009 | 602001 - 错误码
- 5 接口超时
- 1000 服务端系统错误
- 100001 json数据解析错误
- 100002 错误信息json解析失败
- 600003 网络中断
- 600009 URL格式不合法
- 602001 request系统错误
errSubject string - 统一错误主题(模块)名称
data any | null - 错误信息中包含的数据
cause Error | null - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - -
# RequestOptions 兼容性
Android iOS web
withCredentials x x 4.0

# 返回值

类型
RequestTask

# RequestTask 的方法

# abort()

中断网络请求。

# abort 兼容性
Android iOS web
3.9 4.11 4.0
# 参见

# request 兼容性

Android iOS web
3.9 4.11 4.0

# 参见

相关 Bug

# 示例

hello uni-app x

Template

Script

<template>
  <view style="flex: 1;">
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-common-mt" style="border-width: 2px;border-style: solid; border-radius: 4px;">
        <textarea :value="res" class="uni-textarea" style="width: 100%;"></textarea>
      </view>
      <view>
        <text>地址 : {{ host + url}}</text>
        <text>请求方式 : {{method}}</text>
      </view>
      <view class="uni-btn-v uni-common-mt">
        <button type="primary" @click="sendRequest">发起请求</button>
      </view>
    </view>
    <scroll-view style="flex: 1;" show-scrollbar="true">
      <view style="padding: 20px;">
        <text>设置请求方式</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
          <button style="padding: 5px; margin-right: 10px;" type="primary" size="mini"
            @click="changeMethod('GET')">GET</button>
          <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
            @click="changeMethod('POST')">POST</button>
          <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
            @click="changeMethod('PUT')">PUT</button>
          <button style="padding: 5px; margin-right: 10px;" type="primary" size="mini"
            @click="changeMethod('DELETE')">DELETE</button>
          <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
            @click="changeMethod('PATCH')">PATCH</button>
          <button style="padding: 5px;margin-right: 10px;" type="primary" size="mini"
            @click="changeMethod('OPTIONS')">OPTIONS</button>
          <button style="padding: 5px;" type="primary" size="mini" @click="changeMethod('HEAD')">HEAD</button>
        </view>
      </view>
      <view style="padding: 20px;">
        <text>请求返回错误码的接口(默认为GET)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in errorCodeUrls" :key="index"
            @click="changeUrl(item)">{{item}}</button>
        </view>
      </view>
      <view style="padding: 20px;">
        <text>请求不同header的接口(默认为GET)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in headerUrls" :key="index"
            @click="changeUrl(item)">{{item}}</button>
        </view>
      </view>
      <view style="padding: 20px;">
        <text>请求不同content-type的接口(默认为GET)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in contentTypeUrls" :key="index"
            @click="changeUrl(item)">{{item}}</button>
        </view>
      </view>

      <view style="padding: 20px;">
        <text>POST请求(有body)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in postUrls" :key="index"
            @click="changeUrlFromPost(item)">{{item}}</button>
        </view>
      </view>
    </scroll-view>
  </view>
</template>


# cookie管理

  • uni-app x 4.0+,app-android平台的 uni.requestuni.uploadFileuni.downloadFile,之间支持共享cookie Cookie共享介绍

# 注意事项

  • app-android平台 request 接口内部通过特殊方式读取了泛型类型,不支持传入动态的泛型:比如将外层方法的普通泛型参数传入 request。
  • 如果使用泛型先创建RequestOptions实例,再传入uni.request(),此时请务必确保request要显式指定泛型,例:
    const options: RequestOptions<Person> = ...
    uni.request<Person>(options)
    
  • app-android平台 uni.request()暂未支持Promise,返回值是RequestTask。
  • web平台 request接口目前没有返回UTSJSONObject类型数据而是一个普通对象,后续可能会调整为UTSJSONObject类型,如需兼容多端可以使用下标获取request返回的数据内容。
  • web平台 request接口目前不支持创建传入的泛型的实例

由于uni-app x的强类型,导致联网相关开发有一些不同,请参考完整教程:uni-app x的联网教程

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 描述
errMsg string - 错误信息