Request with Axios and React blocked by CORS policy

0

I'm trying to make the request for an API, using Axios:

  axios.post('http://api.teste.com.br/v1/getToken', {
      withCredentials: true,
      auth: {
          username: 'usuario',
          password: 'password'
      },
      headers: {                  
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Headers": "Authorization", 
          "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE" ,
          "Content-Type": "application/json;charset=UTF-8"                   
      },
  }).then(resp => {  
    console.log(resp)
  })
  .catch(error => {          
      console.log(error)       
  })

But it always presents the error:

  

Access to XMLHttpRequest at ' link ' from origin '#

Is there any more configuration to do? Remembering that it is a third-party API and I can not change CORS on the server, and PostMan works without problems.

    
asked by anonymous 12.12.2018 / 13:57

1 answer

0

Good afternoon!

Problem solving. I created a proxy in the package.json file

"proxy": "http://api.teste.com.br/v1" and I made the request like this:

 axios.post('http://localhost:3000/getToken',  {username : "user", password : "password"})
  .then(resp => {  
      console.log(resp.data.data.authorization)
  })
  .catch(error => {          
      console.log(error)       
  })

In this way the proxy half deceives CORS and has no more problems.

    
12.12.2018 / 16:07