小程序-fly请求

 下载wx.js源码:https://github.com/wendux/fly/tree/master/dist/npm/wx.js

const app = getApp()
var Fly = require("./wx.js") //wx.js为您下载的源码文件
var fly = new Fly();
//添加拦截器
fly.interceptors.request.use((config, promise) => {
  //给所有请求添加自定义header
  if(false){
    config.headers["X-Tag"] = "flyio";
    return config;
  } 
});
//配置请求基地址
// fly.config.baseURL = 'http://gank.io/api/data/%E7%A6%8F%E5%88%A9'
Page({
  data: {

  },
  postFly() {
    var data = {};
    data.stage_id=1;
    data.page=1;
    data.limit=2;
    fly.post('http://www.qubaobei.com/ios/cf/dish_list.php', data)
      .then(function(response) {
        console.log(response);
      })
      .catch(function(error) {
        console.log(error);
      });
  },
  testFly() {
    //发起get请求
    fly.get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&page=1&limit=2", {

    }).then((d) => {
      //输出请求数据
      console.log(d.data)
      //输出响应头
      console.log(d.header)
    }).catch(err => {
      console.log(err.status, err.message)
    })

  },
  onLoad: function() {
    // this.testFly();
  },
})

 

你可能感兴趣的:(微信小程序)