drh19790711 2017-10-01 16:40
浏览 2194

ReactJS发送了GET请求,响应为302重定向,收到了CORS错误

This is my stack:

  1. Frontend server: Node + ReactJS (f.com)
  2. Backend server (API): Golang (b.com)

I'm doing this in development environment where I use create-react-app to run my frontend server via npm start command. I want to perform authentication using SSO served at sso.example.com/login. Ideally whenever there's a request to the Backend server without proper authorization, it will be responded with HTTP 302 Redirect to the SSO page. So the sequence is:

  1. ReactJS sends GET request to b.com/users
  2. Golang responded with http.Redirect("sso.example.com/login")
  3. ReactJS app gets CORS error from sso.example.com of which I don't have control of adding Access-Control-Allow-Origin:* respond header

How can I redirect my GET request successfully?

  • 写回答

1条回答 默认 最新

  • doujia4619 2017-10-01 17:22
    关注

    In your case, it would be easier to do the redirection in browser on the client side, so you won't have CORS issues (because you are accessing it from the SSO website url directly).

    You can either do it with react, which is complicated: Programmatically navigate using react router V4

    Or do it with window with plain JavaScript, which is easy but might cause problems with browsing history:

    window.location.href = "https://www.example.com";
    

    https://appendto.com/2016/04/javascript-redirect-how-to-redirect-a-web-page-with-javascript/

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决