
简体中文
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
param | RequestOptions<T> | 是 | - | - | 网络请求参数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
data | T | 否 | - | 开发者服务器返回的数据, app-android平台从 4.51版本开始支持ArrayBuffer, app-ios平台从 4.61版本开始支持ArrayBuffer | |
statusCode | number | 是 | - | 开发者服务器返回的 HTTP 状态码 | |
header | any | 是 | - | 开发者服务器返回的 HTTP Response Header | |
cookies | Array<string> | 是 | - | 开发者服务器返回的 cookies,格式为字符串数组 |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
errCode | number | 是 | - | - | 错误码 | |||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | |||||||||||||||||||||||||||
data | any | 否 | - | - | 错误信息中包含的数据 | |||||||||||||||||||||||||||
cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | |||||||||||||||||||||||||||
errMsg | string | 是 | - | - |
类型 |
---|
RequestTask |
中断网络请求。
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
4.0 | 4.41 | 3.91 | 4.11 | 4.11 | 4.61 |
监听 Transfer-Encoding Chunk Received 事件。
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
---|---|---|---|---|---|---|
4.71 | 4.41 | 4.71 | 4.71 | 4.71 | 4.71 | 4.71 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
listener | (result: RequestTaskOnChunkReceivedListenerResult) => void | 是 | - | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
data | ArrayBuffer | 是 | - | 返回的chunk buffer |
类型 |
---|
number |
移除 Transfer-Encoding Chunk Received 事件的监听函数。
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
---|---|---|---|---|---|---|
4.71 | 4.41 | 4.71 | 4.71 | 4.71 | 4.71 | 4.71 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
listener | number | (result: RequestTaskOnChunkReceivedListenerResult) => void | 否 | - | - |
监听 HTTP Response Header 事件。
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
---|---|---|---|---|---|---|
4.71 | 4.41 | 4.71 | 4.71 | 4.71 | 4.71 | 4.71 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
listener | (result: RequestTaskOnHeadersReceivedListenerResult) => void | 是 | - | - | HTTP Response Header 事件的监听函数 |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
cookies | Array<string> | 是 | - | 开发者服务器返回的 cookies,格式为字符串数组 | |
header | UTSJSONObject | 是 | - | 开发者服务器返回的 HTTP Response Header | |
statusCode | number | 是 | - | 开发者服务器返回的 HTTP 状态码 (目前开发者工具上不会返回 statusCode 字段,可用真机查看该字段,后续将会支持) |
类型 |
---|
number |
移除 HTTP Response Header 事件的监听函数。
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
---|---|---|---|---|---|---|
4.71 | 4.41 | 4.71 | 4.71 | 4.71 | 4.71 | 4.71 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
listener | number | (result: RequestTaskOnHeadersReceivedListenerResult) => void | 否 | - | - |
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>
<!-- #ifdef APP || MP-WEIXIN || WEB -->
<view style="padding: 20px;">
<text>ArrayBuffer 测试</text>
<view class="uni-common-pb"></view>
<view style="flex-direction: row;flex-wrap: wrap;">
<button style="padding: 5px;" type="primary" size="mini"
@click="sendArrayBuffer(false)">请求body为ArrayBuffer,response也为ArrayBuffer</button>
<button style="padding: 5px;" type="primary" size="mini"
@click="sendArrayBuffer(true)">请求response为ArrayBuffer</button>
</view>
</view>
<!-- #endif -->
<view style="padding: 20px;">
<text>RequestTask 测试</text>
<view class="uni-common-pb"></view>
<button type="primary" @click="sendChunkRequest">流式请求</button>
</view>
<view class="uni-common-pb"></view>
<view class="uni-common-pb"></view>
</scroll-view>
</view>
</template>
<style>
.uni-textarea {
padding: 9px;
font-size: 14px;
}
</style>
uni.request
、uni.uploadFile
、uni.downloadFile
等网络API之间支持共享cookie Cookie共享介绍。AI大语言模型的服务器,向客户端持续的流式输出AI推理的结果文本。在客户端表现为打字机效果。
在uni-app x中,实现AI聊天的方式如下:
实现AI聊天的工作较为复杂,流式接收和渲染markdown很容易引发性能问题,处理原生的markdown解析工作量也很大。 所以DCloud提供了开源的uni-ai x,已完整实现原生的、全端的AI聊天,详见
由于uni-app x的强类型,导致联网获取JSON,拿到的不是js的object。
默认返回的是UTSJSONObject,UTSJSONObject是UTS提供的JSON数据对象,实现了js的object的JSON相关功能,但又有区别。如果照搬js的object用法会失败。
同时request也支持泛型,返回一个开发者自定义的type。
请不熟悉强类型的开发者务必阅读教程:uni-app x的联网教程
const options: RequestOptions<Person> = ...
uni.request<Person>(options)
complete
回调中置空Task对象,例complete: () => {
this.task = null
},
如不释放,在调用Task对象的方法将导致控制台报错:
error: instance object does not exist: id:15
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |