# Promise callFunction(Object object)

调用云函数

# 参数

# Object object

属性 类型 默认值 必填 说明
name string 是 云函数名
data Object 否 传递给云函数的参数,在云函数中可通过 event 参数获取
config Object 否 配置
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)

object.config 的结构

属性 类型 默认值 必填 说明
env string 是 环境 ID,填写后将忽略 init 时指定的环境 ID

# 返回值

# Promise.<Object>

属性 类型 说明
result any 云函数返回的结果
requestID string 云函数执行 ID,可用于日志查询

# data 参数说明

最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:

  • 对于 GET 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
  • 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会对数据进行 JSON 序列化
  • 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)

# 示例代码

wx.request({
   url: 'test.php', //仅为示例,并非真实的接口地址
   data: {
     x: '',
     y: ''
   },
   header: {
     'content-type': 'application/json' // 默认值
   },
   success (res) {
     console.log(res.data)
   }
 })