微信网页授权获取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;

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM