Turn from:Http://www.tuicool.com/articles/2u2mmmuPost data using Ajax in Laravel 5 to Controller
If you is going to work with the AJAX data post to controller or the route in Laravel 5. There is some need to get Ajax call work correctly. Your requirement is CSRF token. A default feature in Laravel is it's automatic CSRF security. When you working with forms it's automatically add a "_token" hidden field to your form. On each POST request token is matched for csrf protection. So it's one more cool feature provided by Laravel 5.
Why on AJAX post internal server error Laravel:-
If you are using the default-for-Ajax data post, you'll get "Internal server Error" in Laravel 5. Cause missing CSRF token posting with Ajax post data. and error reason is token not being matched on post request. So every time a form data is posting require CSRF token match. else you'll get "Internal server Error" in Laravel.
In this article we'll explore how to solve $ internal server error in Ajax post or call in Laravel or what to post data Using Ajax in Laravel 5. There is many ways to does this but I am sharing II ways:-
1. Adding on each request
2. Globally
How to Post data using Ajax in Laravel 5:-1. Adding on the request and post data to controller:-
In this "We need to" add token on each AJAX call with data which is posting
view:-Add a "login.blade.php" under "resources/views/" and add below code to make a form
<Divclass="Secure" >secure Login form</div>{!! Form::open (' url ' = ' account/login ', ' method ' = ' POST ', ' id ' = ' myform '))!<Divclass="Control-group" ><div class= "controls" > { !! Form::text (' email ', ', ' Array (' id ' = ' = ', ' class ' = ' Form-control span6 ', ' placeholder ' = ' + ' email ')]! </div></div> <div class= "Control-group" > <div class= "controls" > {!! Form::p assword (' Password ', array (' class ' = ' Form-control ' span6 ', ' placeholder ' = ' please Enter your password '))!! </div></div>{!! Form::button (' Login ', Array (' class ' = ' send-btn '))!} {!! Form::close ()!}
Now add your Ajax call or post data script to your layout footer or in the same file.
<script type="text/javascript">$(document).ready(function(){ $(‘.send-btn‘).click(function(){ $.ajax({ url: ‘login‘, type: "post", data: {‘email‘:$(‘input[name=email]‘).val(), ‘_token‘: $(‘input[name=_token]‘).val()}, success: function(data){ alert(data); } }); }); });</script>
routes:-Add your get and post route to "app/http/routes.php"
Route::get(‘account/login‘, function() { return View::make(‘login‘);});Route::post(‘account/login‘, ‘[email protected]‘);
controller:-Add a controller to "App/http/controllers" with named "accountcontroller.php" and add below code
<?php namespace App\Http\Controllers;use Input;use Request;class AccountController extends Controller { public function login() { // Getting all post data if(Request::ajax()) { $data = Input::all(); print_r($data);die; }}
After all make go to your page URL and click on button and you get your data have been posted and you'll get alert with s Uccess.
2. Globally:-
The This is the we'll add token for globally work with Ajax call or post. So no need-to-send it with Data post.
1. Add a meta tag to your layout header:-Csrf_token () 'll be the same as "_token" CSRF tokens that Laravel automatically Adds in the hidden input on every form.
<meta name="_token" content="{!! csrf_token() !!}"/>
2.Now add below code to footer of your layout, or where it would set for globally or whole site pages. This would pass token to each AJAX request.
<script type="text/javascript">$.ajaxSetup({ headers: { ‘X-CSRF-Token‘ : $(‘meta[name=_token]‘).attr(‘content‘) }});</script>
Now make a Ajax POST request an is done your data would post successfully.
Post data using Ajax in Laravel 5