1.数据请求:
wx.request(OBJECT)
wx.request发起的是 HTTPS 请求。最多能同时进行五个请求.
参数名 |
类型 |
必填 |
说明 |
url |
String |
是 |
开发者服务器接口地址 |
data |
Object、String |
否 |
请求的参数 |
header |
Object |
否 |
设置请求的 header , header 中不能设置 Referer |
method |
String |
否 |
默认为 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT |
dataType |
String |
否 |
默认为 json。如果设置了 dataType 为 json,则会尝试对响应的数据做一次 JSON.parse |
success |
Function |
否 |
收到开发者服务成功返回的回调函数,res = {data: ‘开发者服务器返回的内容‘} |
fail |
Function |
否 |
接口调用失败的回调函数 |
complete |
Function |
否 |
接口调用结束的回调函数(调用成功、失败都会执行) |
代码展示:
//事件处理函数 bindViewTap: function() { wx.request({ url: ‘http://www.baidu.com‘, //仅为示例,并非真实的接口地址 data: { x: ‘‘ , y: ‘‘ }, header: { ‘content-type‘: ‘application/json‘ }, success: function(res) { console.log(res.data) }, fail: function(res) { console.log("---fail---") } }) },
原文:http://www.cnblogs.com/pengsi/p/6442395.html