重定向到微信授權頁面時調試發現Status Code:301 Moved Permanently並且報錯提示跨域:
XMLHttpRequest cannot load
https://open.weixin.qq.com/connect/oauth2/authorize...#wechat_redirect. Redirect
from 'https://open.weixin.qq.com/connect/oauth2/authorize...#wechat_redirect' to
'https://open.weixin.qq.com/connect/oauth2/authorize...#wechat_redirect' has been
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the
requested resource. Origin 'null' is therefore not allowed access.
在調試界面發現Headers里面出現了兩次重定向,所以才導致了這個問題。
解決方法是在微信提供的授權頁面鏈接中添加connect_redirect=1:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIR
ECT_URI&response_type=code&scope=SCOPE&state=STATE&connect_redirect=1#wechat_redirect
再請求時Status Code:200 OK
