Laravel 5.1 ajax not working?

I am trying to submit a login form using ajax in laravel, but it seems that part of ajax is not working. below is my login.blade.php

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </head> <body> <div class="secure">Secure Login form</div> {!! Form::open(array('url'=>'account/login','method'=>'POST', 'id'=>'myform')) !!} <div class="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::password('password',array('class'=>'form-control span6', 'placeholder' => 'Please Enter your Password')) !!} </div> </div> {!! Form::button('Login', array('class'=>'send-btn')) !!} {!! Form::close() !!} <script type="text/javascript"> $(document).ready(function(){ $('.send-btn').click(function(){ $val=$('input[name=email]').val(); $token=$('input[name=_token]').val(); //alert($val+$token); $.ajax({ url: '/account/login', type: "post", data: {'email':$('input[name=email]').val(), '_token': $('input[name=_token]').val()}, success: function(data){ alert(data); } }); }); }); </script> 

route.php

 Route::get('account/login', function() { return View::make('login'); }); Route::post('account/login', ' AccountController@login '); 

in the controller

  public function login() { if(Request::ajax()) { $data = Input::all(); print_r($data); die; } } 

I tried to warn the input values ​​in the ajax part before success, it will print the output, but the success warning will not work.

0
source share
3 answers

Although registering users through AJAX is unsafe, it can be completed no more than:

 {!! Form::open(array('url'=>'account/login','method'=>'POST', 'id'=>'myform')) !!} /** * Your Form */ ... {!! Form::submit('Login', array('class'=>'send-btn')) !!} {!! Form::close() !!} 

The CSRF field will be automatically created for you. JavaScript part:

 <script> (function($) { $('#myform').submit(function(e) { $.ajax({ type: 'POST', url: 'account/login', data: $('#myform').serialize(), success: function(data) { alert(data); } }); e.preventDefault(); }); })(window.jQuery); </script> 

This should do the trick for the HTML and JavaScript parts. In Laravel 5.1, we can use the Injection method to get the Request Object fields as follows:

 <?php namespace Blah\Blah; use Illuminate\Http\Request; class AccountController { ... public function login(Request $request) { if ($request->ajax()) { // Its AJAX!! return $request->all(); // All the Input fields } } } 

Check out the changes I made to your code. Check if this method works or not.

Hooray!

+1
source

try the following:

 public function login() { if(Request::ajax()) { $data = Request::all(); return $data; } } 
0
source

Token required. Try adding:

: {'X-CSRF-TOKEN': token}

in your ajax request:

 $.ajax({ url: route, headers: {'X-CSRF-TOKEN': token}, type: 'POST', dataType: 'json', data: {data : data} }); 
0
source

All Articles