I have multiple Laravel sites hosted on the same server. With the latest site I've created, the contact form refuses to submit without throwing a 419 error. I have set up the routing in my web.php file just like the other websites, which have live, working contact forms, and I'm generating and sending the token the same way - with {{ csrf_field() }}
.
I found an answer to a similar question stating that you can disable Csrf checking by adding entries to the $except
array in app/Http/Middleware/VerifyCsrfToken.php
. I have verified that this does indeed resolve the 419 error:
protected $except = [
'contact',
'contact*',
];
But of course, I wish to keep the Csrf functionality, and I only updated the $except
array for troubleshooting value.
Does anyone know what may be different about the new Laravel environment that would have this 419 behavior despite passing the generated token? I have tried updating several ENV settings and toggling different things, but nothing other than modifying the $except
array has had any influence on the issue.
Update
Since there has been a bit of discussion so far, I figured I'd provide some additional info and code.
First, this is an Ajax form, but don't jump out of your seat just yet. I have been testing the form both with and without Ajax. If I want to test with Ajax, I just click the button that's hooked up to the jQuery listener. If not, I change or remove the button's ID, or run $("#formName").submit();
in the console window.
The above (ajax, old-fashioned submit, and the jQuery selector with .submit();
) all result in the same response - a 419 error.
And for the sake of completeness, here's my Ajax code which is working on all of the other websites I'm hosting. I defined a postData array to keep it all tidy, and I added a console.log()
statement directly after it to (again) confirm that the token is generated just fine and is being passed correctly with the request.
var postData = {
name: $("#name").val(),
email: $("#email").val(),
message: $("#message").val(),
_token: $("input[name=_token]").val()
};
console.log(postData);
$.post("/contact", postData, function (data) {
...
Any ideas? Could there be a configuration issue with my ENV or another file?
Progress Update!
Because the other sites are working just fine, I cloned an old site and simply overwrite the files that I changed for the new website and bam! It's working now. Doing a little bit more digging, I ran php artisan --version
on the cloned version of the site versus the non-working version, and here are the results:
Working Version: Laravel Framework 5.7.3
Non-working Version: Laravel Framework 5.7.9
Perhaps this is a bug with Laravel? Or perhaps some packages on my server are out of date and need to be updated to work with the new version of Laravel?
web.php
I have the following two routes:Route::get('/contact', 'ContactController@index');
andRoute::post('/contact', 'ContactController@contactSubmit');
_token
value being sent to the POST route?dd($request->all())
in thecontactSubmit
function and make sure it's being sent properly.dd($request->all())
before posting this question, and on my local dev environment it shows the json output correctly, including_token
. However, on the server, it will not even reach thedd()
because it throws the 419 error before executing even the first line of code in thecontactSubmit()
method. But the other contact forms on the other sites hosted on my server work fine without any issue._token
being sent with the request in your browser's web inspector's Network tab?