12

I am currently getting the above error, I am using Axios to make the GET request to an external API. After reading the Mozilla docs, doing a lot of research and trying different options I am still not any better off.

I have stripped the code back to the basics:

axios.get('URL.com', {
        headers: {
          Access-Control-Allow-Origin: *
        },
        auth: {
          username: 'username',
          password: 'password'
        },
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

Do I need to add anything else to the headers?

Everything works through Postman so once I can pass the CORS issue everything will work.

James Parsons
  • 895
  • 5
  • 18
  • 36
  • 2
    You don't include CORS headers in requests made from the browser. [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) needs to be enabled/configured on the server. – cartant Jan 21 '17 at 11:43
  • 1
    I understand now, new to CORS. So I'd need to speak to the developers of the api. – James Parsons Jan 21 '17 at 11:44

1 Answers1

1
baseURL: 'https://www.yourserver.com.br',
timeout: 10000,
withCredentials: false

setting axios.defaults.withCredentials = true;

Leonardo Filipe
  • 1,534
  • 15
  • 9
  • 8
    Greetings. A proper answer here involves more explanation than just the code. Can you say a bit more about your intent? – New Alexandria Oct 27 '17 at 13:26
  • 3
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Oct 27 '17 at 14:56