7

i'm trying to make some requests using axios and the last Laravel version 5.5 after configure the X-CSRF fields and all my code is simple :

        axios.post('/post-contact',{name:'Kamal Abounaim'})
        .then((response)=>{
            console.log(response)
        }).catch((error)=>{
            console.log(error.response.data)
        })

but i get this error : 419 (unknown status) what the problem supposed to be Thanks for answering

5
  • Is this happening after you've been on the page for a while or straight after a page load? Are you getting any other errors in your console? Commented Sep 7, 2017 at 8:37
  • sorry didn't say this comment, no i have no errors if i tried with axios.get it works but with post no i create a plain form and try to post and it works to Commented Sep 7, 2017 at 10:21
  • If you check the network tab in your browser is it definitely including either the X-XSRF OR X-CSRF header in the request or the _token in the body?] Commented Sep 7, 2017 at 11:13
  • i checked it still not working Commented Sep 7, 2017 at 17:27
  • It would be polite to select a right answer. @KamalL'azz Commented Mar 20, 2019 at 18:30

2 Answers 2

14

This is happening because of the csrf-token. Just add meta tag with the csrf-token in the <head> and add that token to axios header like so.

// in the <head>
<meta name="csrf-token" content="{{ csrf_token() }}">

<script type="text/javascript">
    // For adding the token to axios header (add this only one time).
    var token = document.head.querySelector('meta[name="csrf-token"]');
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;

    // send contact form data.
    axios.post('/post-contact',{name:'Kamal Abounaim'
    }).then((response)=>{
        console.log(response)
    }).catch((error)=>{
        console.log(error.response.data)
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

A 419 error seems to be Authentification Timeout. Your code looks fine to me, so it seems the error is with the post-contact endpoint? Try testing that endpoint alone, in a tool like postman.

4 Comments

Thanks bro but with the previous version it was okey is the 5.5 needs new tools or i'm not understanding
Hmm strange, not sure. Hopefully someone else might be able to chip in here. I use Laravel but not upgraded to 5.5 yet.
yeah if you say it, i hate those situations :(
Laravel will throw this error only if you forgot about the CSRF token.

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.