Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  102] [ 4]  / answers: 1 / hits: 24358  / 7 Years ago, mon, may 22, 2017, 12:00:00

Consider following code. It's just a simple http post request with axios library.



axios.post('http://localhost/users', this.state)
.then(function(response) {
if (response.status == 201) {
browserHistory.push('/features');
}
})
.catch(function(error) {
console.log(error);
})


If user enters a wrong data to an input, the response from server holds info, e.g.




  • password has to be longer then...

  • mail missing the @ sign

  • etc...



but unfortunately, I don't know how to get into that response if there's a 400 bad request status. It just shows the error, but I'm unable to get the response.



If the response status is 201, it properly shows the response. But in case of 400, even if I change the condition and add else if (response.status == 400) { console.log(response) } it doesn't show up the response.



Any help highly appreciated.


More From » promise

 Answers
86

Just looking at the axios documentation, it looks like the response should be exposed in the error object (i.e. console.log(error.response)).



More information about different info provided when the response code falls out of 2xx here: https://github.com/mzabriskie/axios#handling-errors


[#57697] Friday, May 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;