英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论