小程序的request 能否全局添加header?

发布于 8 年前作者 xiexiulan18771 次浏览最后编辑 8 年前来自 ask

目前想给所有接口的header添加一个key,不知小程序是否有提供全局处理的方法

5 回复
vkang
vkang1 楼6 年前

在app.js的globalData中定义一个header,然后加一个处理所有请求的函数就可以了。例如:

fetch: function (options) {
    let header = this.header
    return new Promise((resolve, reject) => {
      const requestTask = wx.request({
        method: options.method,
        header: header,
        url: options.url,
        data: options.data,

        success: function (res) {

           resolve(res)

       },

        fail: function (res) {},
        complete: function () {}
      })
    })
  }


tyuan
tyuan2 楼6 年前

like this

fang88
fang883 楼6 年前

const request = function (config = {}) {

       config.header={

       'content-type':'application/x-www-form-urlencoded'

}


wx.requset(config);

}


wenming
wenming4 楼6 年前

开发者自己包装一个 request 即可

wangjing
wangjing5 楼6 年前

自己封装一层