request
获取网络请求任务对象 RequestTask
引入
import { request } from '@ray-js/ray';
参数
Object object
发起 HTTPS 网络请求的回调函数
回调参数 Object res
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
url | string | 是 | 开发者服务器接口地址 | |
data | string | 否 | 请求的参数 | |
header | any | 否 | 设置请求的 header,header 中不能设置 Referer。 | |
timeout | number | 否 | 超时时间,单位为毫秒 | |
method | HTTPMethod | 否 | HTTP 请求方法 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 |
object.success 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
data | string | 开发者服务器返回的数据 |
statusCode | number | 开发者服务器返回的 HTTP 状态码 |
header | any | 开发者服务器返回的 HTTP Response Header |
cookies | array | 开发者服务器返回的 cookies,格式为字符串数组 |
profile | Profile | 网络请求过程中一些调试信息 |
taskId | string | 网络请求 id,用户取消、监听等操作 |
object.fail 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
返回值
RequestTask
获取网络请求任务对象 RequestTask
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。
RequestTask
RequestTask.abort
中断请求任务
参数
Object object
中断请求任务的回调函数
回调参数 Object res
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
函数定义示例
/**
* 中断请求任务
*/
export function abort(params: {
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
complete?: () => void;
/** 接口调用成功的回调函数 */
success?: (params: null) => void;
fail?: (params: {
errorMsg: string;
errorCode: string | number;
innerError: {
errorCode: string | number;
errorMsg: string;
};
}) => void;
}): void;
RequestTask.onHeadersReceived
监听 HTTP Response Header 事件。会比请求完成事件更早
参数
function callback
监听 HTTP Response Header 事件。会比请求完成事件更早的回调函数
回调参数 Object res
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
header | any | 是 | 开发者服务器返回的 HTTP Response Header | |
requestId | string | 是 | 网络请求 id |
RequestTask.offHeadersReceived
取消监听 HTTP Response Header 事件
参数
function callback
取消监听 HTTP Response Header 事件的回调函数
回调参数 Object res
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
header | any | 是 | 开发者服务器返回的 HTTP Response Header | |
requestId | string | 是 | 网络请求 id |
uploadFile
获取上传请求任务对象 UploadFileTask
引入
import { uploadFile } from '@ray-js/ray';
参数
Object object
将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data 的回调函数
回调参数 Object res
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
url | string | 是 | 开发者服务器地址 | |
filePath | string | 是 | 要上传文件资源的路径 (本地路径) | |
name | string | 是 | 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容 | |
header | any | 否 | HTTP 请求的 Header,Header 中不能设置 Referer | |
formData | any | 否 | HTTP 请求中其他额外的 form data | |
timeout | number | 否 | 超时时间,单位为毫秒 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
object.success 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
data | string | 开发者服务器返回的数据 |
statusCode | number | 开发者服务器返回的 HTTP 状态码 |
object.fail 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
返回值
UploadFileTask
获取上传请求任务对象 UploadFileTask
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。