1

I am using axios to post form data to Laravel.

Currently I am able to access the response if successful, however I'm having trouble accessing the error response. In dev tools, I can see my network > response has the following:

{"success":false,"data":{"message":"Token mismatch","refresh":true}}

I'm trying to access refresh to do a boolean comparison, so if its true the page will reload. How do I access this?

I've tried the following:

console.log(error.response);
console.log(error.response.data);
console.log(error.response.data.message);
console.log(error.data);
console.log(error);

Which log as the following respectively:

[object Object]
[object Object]
undefined
undefined
Error: Request failed with status code 419
1
  • Parse the JSOn in order to work with the returned data Commented Sep 16, 2019 at 11:18

3 Answers 3

4

It seems that your server response contains data key. Axios also uses this key to in order to separate application data from HTTP protocol status and headers.

Assuming that you want to get message from the error response (that is "Token mismatch") try to access it using the following path:

error.response.data.data.message

Sign up to request clarification or add additional context in comments.

Comments

1

Add

{{csrf_field()}} 

With your post request to avoid 419 error

2 Comments

Thanks, yes I already have a csrf field, but for the purpose of testing (removing cookies to bypass an age gate) I had to catch this error to force a page refresh / or do something else.
Ok thanks. I think you have found your answer. 🙂🙂
0

As per the [https://github.com/axios/axios#handling-errors] documentation the error is available in the response. use try catch instead

try {
    apiRes = axios.get('https://www.apponative.con');
  } catch (err) {
    console.error("Error response:");
    console.error(err.response.data);    // ***
    console.error(err.response.status);  // ***
    console.error(err.response.headers); // ***
  } finally {
    console.log(apiRes);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.