Golang重定向来自JS调用的CORS错误。

huangapple go评论83阅读模式
英文:

Golang Redirect from JS call CORS error

问题

我的golang函数在被javascript的fetch函数调用时没有进行任何重定向。这是我之前问题的延伸,之前的问题太宽泛了。

这是我的javascript fetch函数调用golang api的代码:

let config = {
  method: 'GET',
}

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

这是我的golang函数尝试在之后进行重定向的代码:

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)
}

预期结果是页面重定向到域外的页面,但我在网络中始终收到以下错误:

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.

我不能使用no-cors模式,因为我需要重定向到那个页面,甚至获取一个json返回。

我确保golang服务器的头部信息是正确的,并允许javascript的来源(localhost:3000)。我还确保Facebook的oauth api允许golang服务器的域名(localhost:7001)。然而,当访问localhost域之外的域时,重定向总是被阻止。

英文:

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. https://stackoverflow.com/questions/36924580/facebook-oauth-cors-error?noredirect=1#comment61411314_36924580

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

得分: 1

你不能这样做,你必须实际重定向浏览器(在弹出窗口或其他方式中打开)。

英文:

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

huangapple
  • 本文由 发表于 2016年4月29日 23:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/36942583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定