Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in console of AXIOS

I am using axios in reactjs application. My code is like below

axios.post('/api/addresses/upload/',formData,config)
  .then(function (response) {

  })
  .catch(error => {
  });

I did not use any console.log() statement any where but I am getting below error in console.

enter image description here

How this error printed in console ?

like image 572
abu abu Avatar asked Jun 13 '18 11:06

abu abu


People also ask

How do I get error codes from Axios?

import axios from 'axios'; async function makeRequest() { try { const res = await axios. get('https://example.com/does-not-exist'); const data = res. data; console. log(data); } catch (err) { if (err.

How do I get errors in Axios react?

If you know a request is supposed to be quick and you want to give the user quick feedback, you can set a hard timeout in the config passed to axios. get . Axios will throw an error here when the request times out and it'll have an error message like timeout of 30000ms exceeded .

Does Axios throw 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. See the axios docs for more information.

Why we use Axios in node JS?

Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.


1 Answers

Try by setting the header for the axios request,

The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.

May be you will be missing some of your required fields which is mandatory while processing the operations like insertion or updation of a record in your Database. May be you can have the look at it. If everything works well then try the following settings to the header of your request.

If your submitting the form with files then use the header's content-type as

  1. 'multipart/form-data',
  2. without files means then set the content-type as 'application/x-www-form-urlencoded',
  3. If you need to post the json means then set the content-type as 'application/json' and "Accept: 'application/json'"

If any cors error occurs then use 'crossDomain': true

var formData = new FormData();
axios.post('/api/addresses/upload/', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})
like image 163
Jeeva Avatar answered Oct 29 '22 02:10

Jeeva