Home > front end >  How to solve net::ERR_CONNECTION_REFUSED or net::ERR_CONNECTION_RESET in axios?
How to solve net::ERR_CONNECTION_REFUSED or net::ERR_CONNECTION_RESET in axios?

Time:04-04

Current url: http://127.0.0.1:8000/

When I trying to send request to http://127.0.0.1:9000/, it provides net::ERR_CONNECTION_REFUSED or net::ERR_CONNECTION_RESET. But http://127.0.0.1:9000/ works properly when I trying to call separately in browser. Other APIs are working properly. What is the reason for this? How can I solve this issue?

My Code:

axios.get('http://127.0.0.1:9000')
     .then((response) => {
        console.log(response);
      })

vue 3.2.31

axios 0.25.0

CodePudding user response:

We get a CORS Policy Error because we have these default headers in our bootstrap.js

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

You should remove default headers for one request in axios. You can use the transformRequest in the request you are making:

axios.get('xxxx.com', { transformRequest: [(data, headers) => {
   delete headers.common['X-Requested-With'];
   return data 
}] })
.then((response) => {
   console.log(response);
})
  • Related