Wechat applet wx.request gets response header

Keywords: Session JSON network

In the network request, sometimes we need to judge or operate according to some information in the response header. How to implement it in the applet?
RequestTask = wx.request(Object object) can be implemented through the RequestTask object.
Note: the basic library 1.4.0 is supported, and the lower version needs to be compatible.

RequestTask.abort()
Interrupt request task

RequestTask.onHeadersReceived(function callback)
Listen for HTTP Response Header events. Earlier than the requested completion event

RequestTask.offHeadersReceived(function callback)
Cancel listening for HTTP Response Header events

   const requestTask = wx.request({
          url: url,
          method: method,
          data: data,
          header: header,
          dataType: dataType,
          success: function(res) {
            isOutTime = false;
            if (res.statusCode != 200) {
              res = (typeof res == "string") ? JSON.parse(res) : res;
              typeof(fcb) == "function" && fcb(res);
              return;
            }
            let data = (typeof res.data == "string") ? JSON.parse(res.data) : res.data;
            typeof(scb) == "function" && scb(data || {});
          },
          fail: function(res) {
            res = (typeof res == "string") ? JSON.parse(res) : res;
            typeof(fcb) == "function" && fcb(res);
          },
          complete: function() {
            if (isOutTime) {
              wx.showToast({
                title: 'request timeout',
                icon: 'loading',
                duration: 2000
              })
              if (url.indexOf('/notAuth/') < 0 || url.indexOf('/servlet/StaffServlet') < 0) {
                // wx.clearStorageSync("username");
              }else{
                var i = setInterval(
                  function() {
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                    clearInterval(i);
                  }, 2000);
              }
              console.log("request timeout:"+url)
            }
            isOutTime = false;
            if (!o.hidetoast) {
              that.loading._hide();
            }
            typeof(ccb) == "function" && ccb();
          }
        });
        requestTask.onHeadersReceived((res) => {
          console.log(url)
          console.log("Got it. header");
          if (res.header && res.header["Set-Cookie"]) {
            if (url.indexOf('/notAuth/') > -1 || url.indexOf('/servlet/StaffServlet') > -1) {
              console.log("session keep")
            } else {
              console.log("session Invalid")
              wx.showToast({
                title: 'session Invalid',
                icon: 'loading',
                duration: 2000
              })
              requestTask.abort();
              var i = setInterval(
                function() {
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                  clearInterval(i);
                }, 2000);
            }
          }
        })

The above is the unified method of hhtp request encapsulated in my project. When a network request is initiated, when it is found that the timeout of the request and the restart of the server cause the session to fail, it will automatically return to the login page.

Posted by dennisjade on Wed, 13 Nov 2019 11:11:08 -0800