问题描述:项目使用webSocket通讯,在微信开发者工具里能获取到数据,但是在手机端如果不开启调试的话就无法获取到数据。不知道是不是webSocket通讯的问题,然后我就试了获取用户的openid,想让它显示在index.wxml,结果也是一样(不开启调试也是不能获取到用户的openid)。给出获取openid的代码:
wx.login({ success: function (res) { //调用request请求api转换登录凭证 wx.login({ success: function (res) { //调用request请求api转换登录凭证 wx.request({ url: 'https://api.weixin.qq.com/sns/jscode2session?appid=000&secret=1111&js_code=' + res.code + '&grant_type=authorization_code' , header: { 'content-type' : 'application/json' }, success: function (res) { //数据缓存openid wx.setStorage({ key: "openid" , data: res.data.openid }) }, fail: function (res){ console.log( "require fail:" ,res) } }) } }) } }) |
请问这是什么原因导致的?
楼主怎么处理的?我也遇到这情况啦。找半天没找到原因。。。。。
额,能说明白点吗,我没懂你的意思?这样做的理由是什么?不会又要在index.js里又来让post请求得到这个数据吧???
你把CODE传到后台 在后台获取OPENID
以PHP为例:
$weixin = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=&secret=&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
$array = get_object_vars($jsondecode);//转换成数组
$openid = $array['openid'];//输出openid