yangshousheng

微信网页授权获取code

<script>
var code = GetQueryString(\'code\');
var callback = \'personal.html\';
var appId = "wx70c81e11981cab3d";
var redirect_uri = window.location.origin + window.location.pathname;
var wx_link = \'https://open.weixin.qq.com/connect/oauth2/authorize?appid=\' + appId + \'&redirect_uri=\' +
redirect_uri + \'&response_type=code&scope=snsapi_base#wechat_redirect\';

console.log(code)
if(code==null){
window.location.href = wx_link;
code = GetQueryString(\'code\');

}else{

$.ajax({
url: \'http://www.iopan.cn/familytrip/f/wxlogin\',
type: \'POST\',
async:true,
xhrFields:{
withCredentials:true
},
data: {
code:code
},
success: function(res){
if(res.status==200){
window.location.href="personal.html"
}else{
console.log(res)
return false;
}
},
error: function(){
alert(\'服务器发生错误!\');
}
});

}


function GetQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;

}

分类:

技术点:

相关文章: