Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nobisnews's avatar

React.js and Laravel: TokenMismatchException

Hello everyone,

I'm using React.js as my frontend framework, and Laravel as a pretty much separate backend API (following https://github.com/sexyoung/laravel-react-webpack). I can send GET requests to my backend Laravel API, but when it comes to ajax POST requests, I get the TokenMismatchError.

This is understandable since I don't provide the XSRF token in the header of my ajax request, but I was wondering how to get the XSRF token key in React. Since I'm not using Blade templating, I can't just use {{ csrf_token() }}. Any one have similar problems, or any suggestions? Thanks.

0 likes
2 replies
jimmck's avatar

In your post data add _token and the CSRF token value. It changes every time your Laravel session timesout so you have to change it. In a SPA I just look at the error and request a new one on a JWT guarded route. Of course you can just use JWT and turn off the CSRF middleware.

Please or to participate in this conversation.