Network Error when requesting a server via Axios

Request code on the client, testing on the locale (connecting to localhost: 4000):

export const sendData = data => (dispatch) => {
  dispatch(sendDataRequest());
  const url = routes.syncUrl();
  axios.post(url, data)
    .then((response) => {
      console.log(response);
      dispatch(sendDataSuccess({ responseLink: response.data }));
    })
    .catch((err) => {
      console.log(err);
      dispatch(sendDataFailure({ responseError: 'errore del request' }));
    });
};

The code on the server side, the commented-out version with kors headers does not work either:

export default () => {
  const app = new Express();
  app.use(cors());
  /* app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Request-Method', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
  }); */

  app.use(bodyParser.urlencoded({ extended: false }));

  app.post('/', (req, res) => {
    res.send('tipo qualche link dal server');
  });
  return app;
};

Here is the actual error that crashes in the firefox console when trying to request:

 Error
​
columnNumber: 15
​
config: Object { timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
​
fileName: "file:///home/anehoda/projectImap/dist/main.js"
​
lineNumber: 746
​
message: "Network Error"
​
request: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }
​
response: undefined
​
stack: "createError@file:///home/anehoda/projectImap/dist/main.js:746:15\nhandleError@file:///home/anehoda/projectImap/dist/main.js:289:14\n"
​
__proto__: Object { … }

I do not understand what the problem is, on the Internet everywhere they write that the problem is in corss but I have the necessary headers already present as you can see

Author: Артём Негода, 2019-03-14

1 answers

The issue was resolved by adding http:// to the request url . It was "localhost:3000/" and it should be "http://localhost:3000/" . the get request via the browser worked because the browser automatically adds http:// to the address, but Axios does not.

 0
Author: Артём Негода, 2019-03-16 10:41:38