美文网首页
微信小程序设置全局域名和接口请求

微信小程序设置全局域名和接口请求

作者: 天才ok酱 | 来源:发表于2019-03-14 11:01 被阅读0次

设置全局域名

1.app.wxa里面的globalData里设置域名

globalData: {
      URL: '.../../yuming'
     }

2.同样在这个文件里写下

  wxRequest(method, url, data, callback, errFun) {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: {
        'content-type': method == 'GET'?'application/json':'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      dataType: 'json',
      success: function (res) {
        callback(res.data);
      },
      fail: function (err) {
        errFun(res);
      }
    })
  }

3.在其他页面调用时
js前需先引入全局

const app = getApp();
app.wxRequest('GET', url, data, (res) => {
      console.log(res)
      }, (err) => {
        console.log(err)
      })

相关文章

网友评论

      本文标题:微信小程序设置全局域名和接口请求

      本文链接:https://www.haomeiwen.com/subject/kkqqmqtx.html