This has been covered many times on the forum. as an example https://laracasts.com/discuss/channels/laravel/need-some-advice-on-saving-a-ajax-form-data-into-l5 But search for other replies
TokenMismatchException on submitting a form that didn't exist on page load.
Not sure what is going on here.
I have a form that is ajax'd into the page, it does not exist on page load.
When I submit this form, I get the TokenMismatchException error.
If I place the exact same form directly on the page, not ajax'd in, the form works perfectly fine.
I have a the hidden _token field with the correct value in it. It is being sent in the header according to Dev Tools.
Is it not possible to create a form with ajax and then submit it in Laravel, or is there some trick?
Figured it out.
I was developing and using browsersync so I was on localhost.
When the form is ajax loaded onto the page, the action was no longer pointing to localhost, it was pointing to the Laravel site url.
{!! Form::open(['method' => 'POST', 'action' => ['ExportsController@report']]) !!}
will spit out...
<form method="POST" action="//localhost:3000/exports/report" accept-charset="UTF-8">
...when I call the form directly in the view. But when I ajax in this form, it becomes...
<form method="POST" action="//example.com/exports/report" accept-charset="UTF-8">
This is happening even though I'm ajax loading the form onto the page with a relative url, never calling example.com directly.
No idea why this is, but that fixed my issue sort of, as it's only an issue when browsing on localhost.
Please or to participate in this conversation.