0

I'm trying to authenticate over OAUTH API using Axios. The initial request is just a simple GET to get the auth token.

  axios.get(
    "https://github.com/login/oauth/authorize?client_id=$ID"
  ).then((res) => { console.log(res) })

I immediately get:

...from origin 'http://localhost:3001' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I can use an href link and it works totally fine. What could be the issue here?

John Lippson
  • 1,269
  • 5
  • 17
  • 36

1 Answers1

0

In simple terms, when you are using the anchor tag, it is a link to the original site. When user click on a tag, user will be redirected to that site. But when an AJAX request user will stay in your site and sends an ajax request to the server(github in this case).

When using HTTP protocol there is a header call origin which will tell the backend server where user is from, see the below picture enter image description here

So if server does not allow sources other than it self, this security check will be failed and the AJAX request won't be success. Please let me know if you need more clarifications and I'll be glad to help. Hope that helps.

Janith
  • 2,730
  • 16
  • 26