求助php返回后台json对象数据,在真机上不被解析
发布于 5 年前 作者 rding 2942 次浏览 来自 问答

php后台代码:

//  header(‘Content-Type:application/json; charset=UTF-8’);

//header(‘Content-Type:application/json; charset=UTF-8’);

      if ($this->data) {      

          $check = M(‘user’)->where([‘openid’=>$this->data->openId])->find();

          $id = 0;

 

          if(!$check){

              this->data->openId;

              this->data->nickName;

              this->data->gender;

              this->data->avatarUrl;

              $data[‘add_time’] = time();

 

              $id = M(‘user’)->data($data)->add();

 

              if(!$id){

                  echo json_encode(array(‘code’=>0,‘message’=>‘注册失败!’));

                  exit();

             }

 

          }else{

              this->data->nickName;

              this->data->avatarUrl;

              $data[‘last_time’] = time();

 

              $save = M(‘user’)->where([‘openid’=>$this->data->openId])->data($data)->save();

 

              if(!$save){

                  echo json_encode(array(‘code’=>0,‘message’=>‘更新失败!’));

                  exit();

              }

              check[‘id’];

          }

 

                 id;

                 this->data->session_key;          

 

                  //设置令牌

                this->saveToCache($arr);

                if($rs){

                    //rs;

 

                    echo json_encode(array(‘code’=>1,‘message’=>‘操作成功’,‘data’=>$rs));

                    exit();

 

                  }else{

                      echo json_encode(array(‘code’=>0,‘message’=>‘授权失败!’,‘data’=>’’));

                      exit();

                  }

 

      }else{

          echo json_encode(array(‘code’=>-1,‘message’=>‘暂无数据’));

          exit();

      }

——————————————————————————

小程序代码:

wx.login({

success: function (res) {

if (res.code) {

var code = res.code;

wx.getUserInfo({

success: function (res) {

console.log(res)

wx.request({

url: https://,

method: ‘post’,

datatype: ‘json’,

data: {

code: code,

rawdata: res.rawData,

encrypteddata: res.encryptedData,

iv: res.iv,

signature: res.signature

},

header: {

‘content-type’: ‘application/x-www-form-urlencoded’

},

success: function (rs) {

var data = rs.data;

console.log(rs)

console.log(typeof rs)

console.log(rs.data)

console.log(22222222222)

console.log(rs.data.code)

console.log(rs.data.data)

if (data.code == 1) {

wx.hideLoading();

wx.setStorageSync(“sessionkey”, data.data);

wx.setStorageSync(“user_info”, {

nickname: res.userInfo.nickName,

avatar_url: res.userInfo.avatarUrl,

});

} else if (data.code == -1) {

wx.showToast({

title: ‘网络异常请重试’,

duration: 2000

});

} else {

console.log(1211212121)

wx.showToast({

title: data.message

});

}

console.log(wx.getStorageSync(‘user_info’));

}

});

},

fail: function (res) {

wx.hideLoading();

that.getauth({

content: ‘需要获取您的用户信息授权,请到小程序设置中打开授权’,

cancel: true,

success: function (e) {

if (e) {

that.login();

}

},

});

}

});

} else {

//console.log(res);

}

}

});

小程序测试工具输出

真机输出:

但是就是获取不到json对象下面的数据  res.data.code会输出null

5 回复

最后改了下代码,

console.log(typeof rs)这是一个object

console.log(typeof rs.data)   这里面输出了string了

这是结果

先把BOM头去掉试试

我是用notepad++打开的,默认是无BOM的utf-8

求助,真的试过任何办法了,这套逻辑代码,在几个项目都用过,但是这个出现了这种问题,不知道哪里出现了bug

回到顶部