1

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
  • As @OneOfOne said in an answer, you can't do that. Your JS code will have to issue the redirect, and only if it is embedded JS on the same site. Else, you'll get a CORS error. Commented Apr 29, 2016 at 16:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I initially handled redirects in the server because that was how I initially learned oauth2. I thought the server was to handle the key exchanges AND the redirects. But with a javascript front end, i can just fetch the url myself. So I just used window.location with the parsed URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.