分享者:心存善念,来自原文地址
1.wx.request
wx.request发起的是 HTTPS 请求,以下是它的基本用法,这里不再啰嗦
https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html#wxrequestobject
2.封装GET
在utils下创建httpclient.js
var app = getApp();
function get(param) {
var mydata = {};
var header = param.header;
if(!header){
header = {
'content-type': 'application/json'
}
}
mydata = param.data || {};
mydata['sessionId'] = app.globalData.sessionId;
wx.request({
url: app.globalData.serverAddress + param.url,
data: mydata,
header : header,
method: "GET",
success: param.success,
fail: param.fail,
complete: function () {
// complete
if(param.complete){
param.complete();
}
}
})
}
module.exports = {
get: get
}
调用方法示例
var httpClient = require('../../utils/httpclient');
httpClient.get({
url: 'Account/Get',
data: this.data.userInfo,
success: function (res) {
//this is success
},
fail: function () {
that.showTopTips('遇到错误!');