duanhe6464 2016-04-29 15:45
浏览 87
已采纳

Golang从JS调用CORS错误重定向

My golang function does not redirect anywhere when called by a javascript fetch function. This is an extension of my previous question which was too broad. Facebook Oauth CORS error

This is my javascript fetch function calling the golang api.

let config = {
  method: 'GET',
}

fetch("http://localhost:7001/testFunction", config).then(response => {
  ...

This is my golang function trying to redirect afterwards

func testfunc(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w,r,"https://www.facebook.com/dialog/oauth?..", 302)
    http.Redirect(w, r, "http://localhost:7001/", 302)
    http.Redirect(w, r, "http://google.com/", 302)
}

The outcome is supposed to be that the page redirects to a page outside the domain, but I consistently get this error on network.

Fetch API cannot load http://google.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I cannot use no-cors mode because I need to redirect to that page or even retrieve a json back.

I made sure the golang server headers were correct and allowed the javascript origin (localhost:3000). And I made sure that the oauth api on facebook allowed the golang server domain (localhost:7001). Yet the redirects are always blocked when accessing domains outside of the localhost domain scope.

  • 写回答

1条回答 默认 最新

  • douba6361 2016-04-29 16:02
    关注

    You can't do that, you will have to actually redirect the browser (open it in a pop window or something).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services