S.M_Emamian S.M_Emamian - 2 years ago 92
PHP Question

how to post to a route url in laravel

I'm using this route :

Route::post('cp/admin/checkUserPassLogin','Panel\[email protected]');

and my controller :

enter image description here

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use DB;
use App\Quotation;
public function checkUserPassLogin(Request $request)
echo 'test';
return 'test';


and in the my view:

function login()
var email = $('#email_admin').val();
var pass = $('#pass_admin').val();

if(!email || !pass)
return ;

$.post("{{ url('/cp/admin/checkUserPassLogin') }}", {username:email,pass:pass}, function (data) {


if(data == 'ok')
window.location.href = "..........";



prints my values but
doesn't return anything .

Answer Source

Undoubtedly, is a CSRF problem. In Laravel 5, all requests must pass through the Middleware which will not allow any POST requests without the correct CSRF token. To solve this problem:

Somewhere in your view add:

<meta name="csrf-token" content="{!! csrf_token() !!}">

Then in your ajax post data add :

'_token': $('meta[name="csrf-token"]').attr('content')

It will look like this:

$.post("{{ url('/cp/admin/checkUserPassLogin') }}", {username:email, pass:pass, '_token': $('meta[name="csrf-token"]').attr('content')}, function (data) {...});

One alternative, is to add the csrf-token directly to your javascript like you did with the URL:

$.post("{{ url('/cp/admin/checkUserPassLogin') }}", {username:email, pass:pass, '_token': "{!! csrf_token() !!}"}, function (data) {...});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download