廢話不多說,直接上解決方案。
首先打開微信公衆平臺頁面,左邊列表找設置->公衆號設置, 選擇功能設置選項卡,確認一下是否正確設置了回調域名(圖中網頁授權域名)
搜索這個問題的人絕大部分應該都已經設置好了,
如果這裏設置了域名,並且你的redirect_uri 給的也是 域名/xxx這種形式, 但是還是訪問不到,首先看一下代碼:
var redirect_uri = "wwww.aaa.com"
var oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize"
+"?appid=" + appid
+"&redirect_uri=" + redirect_uri
+"&response_type=code&scope="+ scope
+"&state=STATE#wechat_redirect"
這裏有個驚天大坑, 在設置回調域名的時候,微信明確指出不要添加http://前綴,所以我們就可能先入爲主的認爲redirect_uri也不需要填寫http://, 如果不填的話是訪問不到的。
解決方法
redirect_uri 帶上“http://”前綴
var redirect_uri = "wwww.aaa.com"
var oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize"
+"?appid=" + appid
+"&redirect_uri=http://" + redirect_uri //注意這裏加上了http://前綴
+"&response_type=code&scope="+ scope
+"&state=STATE#wechat_redirect"