天天看點

小程式實作請求接口資料

代碼實作

1、建立檔案請求接口域名

小程式實作請求接口資料

 2、封裝接口

  2-1、方法一:建立一個api.js

var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
  return typeof e;
} : function(e) {
  return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
}, _rewx = require("./rewx.js"), app = getApp(),IndexData = function (i) {
  return (0, _rewx.post)("/applet/index/index", i).then(function (o) {
    return new Promise(function (e, n) {
      e(o);
    });
  });
},      
orderList= function (i) {
  return (0, _rewx.post)("/applet/order/index", i).then(function (o) {
    return new Promise(function (e, n) {
      e(o);
    });
  });
}      
module.exports={ IndexData:IndexData ,orderList:orderList      
}      

   目錄檔案調用

(0, _api.IndexData)().then(function (t) {
   console.log(t)
   that.setData({

   })
});      

   2-2、方法二:目錄檔案直接使用(get/post請求及帶參數請求)

(0, _rewx.post)("/applet/store/getdetail", { store_id: 1 }).then(function (t) {
    console.log(t)
    that.setData({
       
    })
});

(0, _rewx.get)("/applet/index/index").then(function (t) {
  console.log(t)
  that.setData({
        
  })
});      

   2-3、方法三:直接請求

wx.request({
      url: 'https://xxx.com/applet/store/getdetail', 
      data: {
        x: ''
      },//get/post請求參數
      method:"POST",
      header: {
        'content-type': 'application/json' // 預設值
      },
      success: function(res) {
        console.log(res.data)
      }
    })      

繼續閱讀