微信小程序 (applet of WeChat)

微信小程序 网络请求 wx.request()

2019-07-24  本文已影响0人  Rising_life

wx.request(Object object)

发起 HTTPS 网络请求。

参数(Object object)

url:开发者服务器 wss 接口地址    类型 string    必填

data: 请求的参数     类型 string/object/ArrayBuffer

header: 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json   类型 Object

method: HTTP 请求方法      类型  string    默认值 GET    

dataType : 返回的数据格式      类型  string     默认值 json 

responseType: 响应的数据类型      类型  string     默认值  text  

success:接口调用成功的回调函数     类型 function

fail:接口调用失败的回调函数     类型 function

complete:接口调用结束的回调函数(调用成功、失败都会执行)     类型 function

method 有效值

OPTIONS 

GET 

HEAD 

POST 

PUT 

DELETE

TRACE

CONNECT 

dataType 有效值

json:返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse

其他:不对返回的内容进行 JSON.parse

success 回调函数

参数 (Object res

data:开发者服务器返回的数据    类型 string/Object/Arraybuffer

statusCode:开发者服务器返回的 HTTP 状态码    类型 number

header:开发者服务器返回的 HTTP Response Header    类型 Object

示例代码

wx.request({

    url: 'test.php', //仅为示例,并非真实的接口地址

    data: {

      x: '',

      y: ''

    },

    header: {

      'content-type': 'application/json' // 默认值

    },

    success (res) {

      console.log(res.data)

    }

  })

上一篇下一篇

猜你喜欢

热点阅读