收藏
回答

小程序网络请求头token一样情况下,为啥小程序获取不到值 apipost上却能获取到值?

//网络请求
function request_WANG(url,method,params{
    return new Promise(function(resolve, reject{
      
        wx.showLoading({
            title'加载中',
        })
      
    var data = {};
    if(params) {
      data = params;
    }
    //   return false
    var contentType = 'application/json';
      
    //默认的网络请求为"GET"   
      if(!method){
          method = 'GET'
      }
      else{
        method = method;
      }
    //   console.log('====================================');
      console.log(wx.getStorageSync('token'));
      wx.request({
        url: BASE_URL_WANG + url,
        data: data,
        method: method,
        //添加请求头
        header: {
          'Content-Type': contentType ,
          'Authorization':'bearer '+ wx.getStorageSync('token')
        },
        //请求成功
        successfunction(res{
          if (res.statusCode == 200 || res.statusCode == 201 || res.statusCode == 204) {
            resolve(res);
          }else {
            //请求失败
            reject("请求失败:" + res.statusCode)
            console.log(res);
            if(res.data.msg == 'user already exists'){
                wx.showToast({
                    title'用户已经存在了!',
                    icon'error',
                    duration2000
                })
            }
            else if(res.data.msg == 'phone already exists'){
                wx.showToast({
                    title'手机号已被注册!',
                    icon'error',
                    duration2000
                })
            }
            else if(res.data.msg == 'invalid user'){
                wx.showToast({
                    title'无效的用户!',
                    icon'error',
                    duration2000
                })
            }
            else{
                wx.showToast({
                    title'出错!',
                    icon'error',
                    duration2000
                })
            }
            
            // resolve(res);
          }
        },
        failfunction(err{
            console.log(err);
          reject("服务器连接异常,请检查网络再试");
        },
        completefunction() {
          wx.hideLoading();
        }
      })
    });
}

//================
                api.POSTgetCode(paramr)
                        .then(res => {
                            console.log('==1');
                            console.log(res);
                            console.log(res.data.data);
                            wx.removeStorageSync('token')
                            wx.setStorageSync('token', res.data.data)
                            console.log(wx.getStorageSync('token'));
                            
                            setTimeout(function(){
                                api.GETuserNormal()
                                .then(res => {
                                    console.log('==2');
                                    console.log(res);
                                    resolve('33')
                                })
                                .catch(err => {
                                    console.log('==2');
                                    console.log(err);
                                })
                            },3000)
                            resolve('22')
                        })
                        .catch(err => {
                            console.log(err);
                        })




回答关注问题邀请回答
收藏

1 个回答

  • 那一抹笑😃 穿透阳光
    那一抹笑😃 穿透阳光
    2023-02-02

    前端代码没有问题,只有看一下后端的获取方式了

    2023-02-02
    有用
    回复 2
    • PanHQ
      PanHQ
      2023-02-02
      找到问题了,后端请求头里没有bearer这个参数
      2023-02-02
      回复
    • 那一抹笑😃 穿透阳光
      那一抹笑😃 穿透阳光
      2023-02-02回复PanHQ
      解决了就好
      2023-02-02
      回复
登录 后发表内容