SoFunction
Updated on 2025-03-01

Solve the problem of inconsistent sessionid in WeChat applet

question

Because the sessionid requested by the applet on the applet is inconsistent, the backend cannot obtain the session

Solution

Get sessionid when logging in

//Save to the interface for the first timesessionidmiddle
success: function (res) {
     ();
     ('sessionid');//Clear cache every time you log in     if ( == "0000") {
      if ([0].value =='student'){
       ({
        title: 'Login successfully',
        icon: 'success',
        duration: 1000
       })
       ('sessionid', ['Set-Cookie']); //Save Cookie to Storage        
      }
     } else {
      ({
       title: 'Login failed',
       icon: 'none',
       duration: 2000
      })
     }
    },

When making a request for the page later, add the value of the Set-Cookie you stored in the request header

({   url: 'https://xxxx/examResult/selectWxListStudentStudyStatistic',
   method: 'post',
   header: {
    'content-type': 'application/x-www-form-urlencoded',//Solve the request for data    'cookie': ("sessionid")//Read sessionid, pass it into the background as a cookie and use PHPSESSID as session_id   },
   success: function (res) {
     ();// Remove loading   },
   fail: function (e) {
    ({
     title: 'The server has an error',
     icon: 'none',
     duration: 2000
    })
   }
  })

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.