重定向到微信授权页面时调试发现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
原文地址:https://www.jianshu.com/p/8feea4d8e1c0
微信公众号OAuth2用户授权报错跨域,Origin 'null' is therefore not allowed access