微信公眾號OAuth2用戶授權報錯提示跨域


重定向到微信授權頁面時調試發現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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM