question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios catch a valid response 201 and treat it as an error

See original GitHub issue

#### Summary

Hello , i am having this weird issue related to axios with laravel , when i submit a form using ajax the status code that i receive from laravel api is 201 or 200 but the axios only catch them as error

axios.post(this.url, formData)
                .then(
                    (response) => {
                        console.log(response.status); // return 201
                        const { data } = response
                        vm.$notify({
                            type: 'success',
                            duration: 10000,
                            speed: 1000,
                            title: $t('labels.' + data.message),
                            text: $t('messages.' + data.message)
                        });
                    }
                )
                .catch(e => {
                    vm.$notify({
                        type: 'error',
                        duration: 10000,
                        speed: 1000,
                        text: e.response
                    });

                    //vm.$emit('form-errors', e.response.data.errors);
                })

the return value from the api is

{success: true, message: "created"}

with status code of 201

#### Context

  • axios version: e.g.: v0.17.1
  • Environment: *e.g.: node v8.4.0, macOs, vue 2.5.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:13

github_iconTop GitHub Comments

39reactions
rkpinheirocommented, Jun 28, 2018

I had the same problem, and i see a dependence unsolved on my .then(). When exploded the exception the flow followed to .catch().

in other words, the request code 201,200…(20*) go to catch() only if there is a error in .then().

29reactions
bm2ilabscommented, Mar 2, 2018

this issue happen when your response have an error , for example when the response is correct but in the then response you do something that throw an error , axios then catch that error , even if the response from the server was success, i hope i am making sense

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get the status code from an HTTP error in Axios?
axios.get('/foo') .catch(function (error) { if (error.response) ... returned from the server, you can add validateStatus: status => true to axios options:
Read more >
Axios catch a valid response 201 and treat it as an error
Summary. Hello , i am having this weird issue related to axios with laravel , when i submit a form using ajax the...
Read more >
Handling Errors | Axios Docs
Handling Errors. axios.get('/user/12345') .catch(function (error) { if (error.response) { // The request was made and the server responded with a status ...
Read more >
axios show status false
Insert data in database successfully but axios response.status === 201 show false Network tab also show 201 status. any one know what i...
Read more >
HTTP status and error codes for JSON | Cloud Storage
Error Response Format. Cloud Storage uses the standard HTTP error reporting format for the JSON API. Successful requests return HTTP status codes in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found