您的位置:首页 > 移动开发 > 微信开发

小程序获取openid与session_key

2018-03-31 11:47 525 查看
wxml 
<button bindtap='getopenid'>获取openID</button> //调用js方法

js方法
getopenid:function(){
wx.login({
//获取code
success: function (res) {
var code = res.code //返回code
console.log(code)
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=' + code + 
'&grant_type=authorization_code', //appid 与 secret为对应小程序项目后台获取
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
var openid = res.data.openid //返回openid
console.log(openid);
}
})
}
})
}

注意:

如果出现此现象
可在详情设置中勾选


即可获取openid
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息