发起一个云托管服务的调用请求。
参数 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| options | object | - | 是 | 请求配置 |
参数 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| path | string | - | 是 | 服务地址 |
| init | object | 见init说明 | 否 | 请求配置对象 |
| success | function | - | 否 | 接口调用成功后的回调函数 |
| fail | function | - | 否 | 接口调用失败后的回调函数 |
| complete | function | - | 否 | 接口调用结束后的回调函数(调用成功、失败都会执行) |
参数 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| method | string | POST | 否 | 网络请求方法,支持 GET/POST/OPTIONS/PUT/DELETE/TRACE/PATCH |
| header | object | {"content-type": "application/json"} | 否 | 请求 Header |
| body | object/array/arraybuffer | - | 否 | 请求的参数 注意 GET 方法的请求不能包含 body 信息 当 method 为 POST 且 body 为 json 格式时,其 CloudID 格式的字段值会被网关替换为解密后的数据 |
| timeout | number | 120000 | 否 | 超时时间,单位为毫秒 |
参数为 Object 类型,属性如下:
属性名 | 类型 | 说明 |
|---|---|---|
| statusCode | number | 返回的 HTTP 状态码 |
| header | object | 返回的 HTTP Response Header |
| data | string/any | 返回的数据(小程序侧返回的是 string,需要 JSON.parse 解析) |
| errMsg | string | "Cloud.callContainer:ok" |
参数为 Object 类型,属性如下:
属性名 | 类型 | 说明 |
|---|---|---|
| errMsg | string | "Cloud.callContainer:fail " + 详细错误信息 |
| errNo | number | 错误码 |
const cloud = xhs.cloud;
// 先初始化云环境
xhs.cloud.init({ envId: 'your-env-id' })
.then(() => {
// 调用云托管服务
return cloud.callContainer({
path: '/example',
init: {
method: 'POST',
header: {
"content-type": "application/json",
},
body: {
example: 'example',
},
timeout: 120000, // ms
}
});
})
.then(({ statusCode, header, data }) => {
console.log('调用成功');
console.log('状态码:', statusCode);
console.log('响应头:', header);
console.log('响应数据:', JSON.parse(data));
})
.catch((err) => {
console.error('调用失败', err.errMsg);
console.error('错误码:', err.errNo);
});
const cloud = xhs.cloud;
// 先初始化云环境
xhs.cloud.init({
envId: 'your-env-id',
success: () => {
// 调用云托管服务
cloud.callContainer({
path: '/example',
init: {
method: 'POST',
header: {
"content-type": "application/json",
},
body: {
example: 'example',
},
timeout: 120000, // ms
},
success: ({ statusCode, header, data }) => {
console.log('调用成功');
console.log('状态码:', statusCode);
console.log('响应头:', header);
console.log('响应数据:', JSON.parse(data));
},
fail: (err) => {
console.error('调用失败', err.errMsg);
console.error('错误码:', err.errNo);
},
complete: () => {
console.log('调用完成');
}
});
}
});
错误信息 | 说明 |
|---|---|
| 调用失败 | 请求路径错误或服务不可用 |
| 请求超时 | 服务响应超时 |