欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序封裝http訪問網(wǎng)絡(luò)庫實(shí)例代碼

 更新時(shí)間:2017年05月24日 15:39:32   作者:Rolan  
這篇文章主要介紹了微信小程序封裝http訪問網(wǎng)絡(luò)庫實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

微信小程序封裝http訪問網(wǎng)絡(luò)庫實(shí)例代碼

之前都是使用LeanCloud為存儲(chǔ),現(xiàn)在用傳統(tǒng)API調(diào)用時(shí)做如下封裝

文檔出處:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html

代碼如下:

var HOST = 'http://localhost/lendoo/public/index.php/';
// 網(wǎng)站請(qǐng)求接口,統(tǒng)一為post
function post(req) { 
//發(fā)起網(wǎng)絡(luò)請(qǐng)求
 wx.request({
 url: HOST + req.uri, 
 data: req.param, 
 header: {
   "content-type": "application/x-www-form-urlencoded"
 },
 method: 'POST', 
 success: function (res) {
  req.success(res.data)
 }, 
 fail: function (res) {
   console.log(res);
 }
 })
}
// 導(dǎo)出模塊
module.exports = { post: post
}

然后前端調(diào)用就可以這樣做了:

var http = require('../../utils/http.js');
...
 http.post({ 
  uri: http.orderListUri, 
  param: {
   third_session: wx.getStorageSync('third_session')
  },  
   success: function (data) {
   that.setData({
    orderList: data
  });
  }
 });

一般對(duì)自己寫的接口給自己用的時(shí)候,method方法或header都是約定好的,所以不用重復(fù)書寫。

header: {
   "content-type": "application/x-www-form-urlencoded"
  },
method: 'POST'

而fail回調(diào)方法也可以統(tǒng)一處理;進(jìn)一步地,也可以對(duì)success回調(diào)里的針對(duì)code值進(jìn)一步判斷,特定錯(cuò)誤碼統(tǒng)一處理,比如跳轉(zhuǎn)登錄頁面等。

經(jīng)過上述處理,是不是變得簡(jiǎn)潔了?

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論