微信網頁授權獲取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