微信开发之小程序开发网络请求(表单提交等)

官网地址:https://developers.weixin.qq.com/miniprogram/dev/api/wx.request.html

网络请求使用的是微信的request

下面贴上网络请求实现的代码

method:支持POST GET PUT DELETE

表单提交设置:'Content-Type':application/x-www-form-urlencoded

/**
   * 请求网络获取列表数据
   */
  getList() {
    var that = this;
    //显示微信加载圈
    wx.showLoading({})
    //这里调用微信的request请求传入url、设置请求方式、可以传输data参数、设置请求头、后面就是可以处理成功和失败的时候的业务
    wx.request({
      url: app.globalData.apiHost + '/getfoodList',
      method: 'GET',
      data: {},
      header: {
        'Accept': 'application/json'
      },
      success: function(res) {
        console.log(res.data.data);
        that.setData({
          dataShow: res.data.data,
          total: res.data.data && res.data.data.length > 0 ? res.data.data.length : 0,
        })
        //对微信加载圈进行隐藏
        wx.hideLoading();
      },fail: function(res) {

      }
    })
  },

下图中你看的showLoading是加载圈可以设置加载圈的title等信息、不设置就显示一个加载的效果、同微信加载效果

 wx.showLoading({})

 wx.showLoading({
      title: '',
      mask: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/90234467