Golang 重定向从 JS 调用 CORS 错误

Golang Redirect from JS call CORS error

我的 golang 函数在被 javascript 提取函数调用时不会重定向到任何地方。这是我之前太宽泛的问题的延伸。 Facebook Oauth CORS error

这是我的 javascript 调用 golang api 的 fetch 函数。

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 服务器 headers 正确并允许 javascript 来源 (localhost:3000)。我确保 facebook 上的 oauth api 允许 golang 服务器域 (localhost:7001)。然而,当访问本地主机域范围之外的域时,重定向总是被阻止。

你不能那样做,你必须实际重定向浏览器(在弹出 window 或其他东西中打开它)。