Support the ongoing development of Laravel.io →
posted 9 years ago
Input Forms
Last updated 2 years ago.
0

You need to add _token input in your form. Its for csrf protection.

<meta name="_token" content="{{ app('Illuminate\Encryption\Encrypter')->encrypt(csrf_token()) }}" />
<script type="text/javascript">
		$(function() {
			$.ajaxSetup({
				headers: {
					'X-XSRF-Token': $('meta[name="_token"]').attr('content')
				}
			});
		});
 	</script>

Add above code in your header and each ajax request will have csrf token by default. Also you need to specify _method for each request because laravel use PUT, PATCH methods that are not supported by default so you need to add another field

<input name="_method" value="PUT/PATCH" >
Last updated 9 years ago.
0

shah587 said:

You need to add _token input in your form. Its for csrf protection.

<meta name="_token" content="{{ app('Illuminate\Encryption\Encrypter')->encrypt(csrf_token()) }}" />
<script type="text/javascript">
  	$(function() {
  		$.ajaxSetup({
  			headers: {
  				'X-XSRF-Token': $('meta[name="_token"]').attr('content')
  			}
  		});
  	});
	</script>

Add above code in your header and each ajax request will have csrf token by default. Also you need to specify _method for each request because laravel use PUT, PATCH methods that are not supported by default so you need to add another field

<input name="_method" value="PUT/PATCH" >

i have changed my ajax handler to:

    <script>
    $(document).ready(function() {
        // Ajax for our form
        $('form.ajax').on('submit', function(event) {
            event.preventDefault();

            var formData = $(this).serialize(); // form data as string
            var formAction = $(this).attr('action'); // form handler url
            var formMethod = $(this).attr('method'); // GET, POST

            $.ajaxSetup({
                headers: {
                    'X-XSRF-Token': $('meta[name="_token"]').attr('content')
                }
            });

            $.ajax({
                type  : formMethod,
                url   : formAction,
                data  : formData,
                cache : false,

                beforeSend : function() {
                    console.log(formData);
                },

                success : function(data) {

                },

                error : function() {

                }
            });

            // console.log(formData);

            return false; // prevent send form
        });
    });
    </script>

added meta:

<meta name="_token" content="{{ app('Illuminate\Encryption\Encrypter')->encrypt(csrf_token()) }}" />

and hidden field with method name:

{!! Form::hidden('_method', 'PUT') !!}

and now error is: _token=SUIVMYuPYFqNxt9qrfm7iaX5DN4Sil38uJpV6CEj&ip=127.0.0.1&name=&email=&homepage=&message= jquery.min.js:4 POST http://laravel5-guestbook/guestbook 422 (Unprocessable Entity)jquery.min.js:4 sendjquery.min.js:4 m.extend.ajax(index):39 (anonymous function)jquery.min.js:3 m.event.dispatchjquery.min.js:3 r.handle

i think an issue somewhere in a verification CSRF token(according to http://stackoverflow.com/questions/27098239/).

0

Here is very simple example of Laravel 5 Ajax Form Submit example : https://hdtuto.com/article/php-laravel-ajax-form-submit-example

I hope it can help you... :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Denis denis Joined 30 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.