How to Fix the CSRF Token Mismatch Exception in Laravel When Sending a PUT Request via jQuery AJAX
Автор: vlogize
Загружено: 2025-09-10
Просмотров: 7
Описание:
Learn how to resolve the common issue of `CSRF token mismatch` in Laravel when making AJAX PUT requests, especially when handling files. Follow our step-by-step guide to ensure smooth form submissions!
---
This video is based on the question https://stackoverflow.com/q/62296415/ asked by the user 'Sijan Bhattarai' ( https://stackoverflow.com/u/7393303/ ) and on the answer https://stackoverflow.com/a/62296499/ provided by the user 'Khalid Khan' ( https://stackoverflow.com/u/11259455/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Laravel CSRF token mismatch exception. Sending "Put" request to a resource route via jQuery ajax
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the CSRF Token Mismatch Exception in Laravel
When working with Laravel, developers often face various challenges. One common issue that can arise is the dreaded CSRF token mismatch exception. This can occur when you try to send a PUT request to a resource route using jQuery AJAX, especially if you’re also trying to upload files. Let's break down the problem and explore a solution in a clear and organized way.
The Problem
In your implementation, you're already handling standard form submissions properly. A typical AJAX call using jQuery might look like this:
[[See Video to Reveal this Text or Code Snippet]]
However, when you attempt to modify this code to support file uploads, you switched to using FormData:
[[See Video to Reveal this Text or Code Snippet]]
This change, while needed for file handling, introduced a new challenge: the CSRF token mismatch exception. This can leave you frustrated and wondering what went wrong.
The Solution
Step 1: Add the CSRF Token in Your Blade File
To solve this problem, you should first ensure that the CSRF token is included in the HTML of your page. You can do this by adding a meta tag within the <head> section of your Blade file:
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Adjust Your AJAX Setup
Instead of appending the CSRF token directly to the FormData, we will configure the AJAX headers to include the CSRF token. This prevents the mismatch issue:
[[See Video to Reveal this Text or Code Snippet]]
Step 3: Remove the Token from the FormData
Next, you’ll want to remove the line that appends the CSRF token to your FormData object since it will now be included in the headers:
[[See Video to Reveal this Text or Code Snippet]]
Step 4: Use the Adjusted AJAX Call
Your final AJAX call to send the PUT request should look like this:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
By following these steps—adding the CSRF token to your Blade file, setting it in your AJAX headers, and adjusting your AJAX call—you can effectively resolve the CSRF token mismatch exception when sending PUT requests via jQuery AJAX, even when handling files. This will streamline your form submissions and enhance the overall experience for your users.
Feel free to integrate these solutions into your development process to avoid unnecessary roadblocks. Happy coding!
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: