Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post request Axios : Network error

I'm using NodeJS for the backend, and ReactJS for the frontend.

I've a problem with request Axios network. All my Get request work. But Post request don't work. I have just this error "network error"

I created a simple webservice to show you my problem :

//Serveur code

helloWs : (request:Express.Request, response:Express.Response) => {
        try {
            response.send('hello WS !')
        } catch (error) {
            console.error(error)
            response.send('error' + error + 'status : ' + error.response.status)     
            response.end()    
        }
    }

//Here I create my root 
router.post('/helloWs',DocumentController.helloWs)

//This is my front

 axios.post('http://localhost:9000/1/documents/helloWs', { 
 }) .catch(function (error) {
                if (error.response) {
                    console.log('Error data : ', error.response.data);
                    console.log('Error status : ', error.response.status);
                    console.log('Error headers : ', error.response.headers);
                } else if (error.request) {
                    console.log('Error request : ', error.request);
                } else {
                    console.log('Error message : ', error.message);
                }
                console.log(error.config);
            })

In the navigator console, I've just network error , and my webservice is in OPTION and not in "POST"

I'm trying to add header in axios, but it doesn't work. I specify that I've tested with postman, and it's okay. Do you have an idea ? Thank you

like image 657
Anaïs Saez Avatar asked Mar 06 '18 14:03

Anaïs Saez


People also ask

What causes network error in Axios?

Due to security constraints on JS in the browser, if you make an API request and it fails due to crappy networks, the only error you'll see is “Network Error,” which is incredibly unhelpful.

How do I send a post request from Axios?

A POST request can be made using Axios to “post” data to an endpoint. This endpoint may then use this POST request to perform a certain task or trigger an event. The HTTP post request is performed by calling axios. post() .

Does Axios catch 400 error?

By default, the axios HTTP library throws an error anytime the destination server responds with a 4XX / 5XX error (for example, a 400 Bad Request ). Since axios raises an error, your workflow will stop at this step.

What is Axios HTTP library?

Axios: Axios is a Javascript library used to make HTTP requests from node. js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS ES6. It can be used intercept HTTP requests and responses and enables client-side protection against XSRF. It also has the ability to cancel requests.


1 Answers

It is most likely a cors related error, the OPTION request is made by the browser before the actual request to check if your domain has the rights to access the resource.

Postman doesn't do the preflight request that's why you get the response

try adding this middleware in your server side code before defining the routes

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
like image 183
Karim Avatar answered Sep 22 '22 14:09

Karim