微信小程序之HTTPS请求

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('遇到错误!');
            }
        });    

 

3.封装POST

post也类似

 

有空再继续继续补充

 

欢迎阅读本系列文章:微信小程序开发教程目录

 

posted @ 2017-04-07 10:36  心存善念  阅读(4569)  评论(0编辑  收藏  举报