Can you post some code? What are you using to perform these requests? Where are you retrieving your token from?
Getting TokenMismatchException in VerifyCsrfToken.php line 68 sometime
Hello,
I'm facing something weird in laravel while calling ajax function i got sometime token mismatch exception while i'm sending the token. So i did the javascript function in a way like i wait 7s to get a response if i don't have a response i add a retry button. When i click retry i got a response without Token mismatch error and sometimes from the first time i got the response.
Have anyone faced a problem like this?
hi, be sure to check if the csrf_token is being passed with the request, if you are using ajax request, it should be passed automatically by default in laravel.
Hello sorry for late reply this is my code:
HTML:
{{ Form::open(array('id'=>'login_form','class'=> 'panel p-a-4')) }}
<label>
<input type="email" name="email" placeholder="Email" />
</label>
<label>
<input type="password" name="password" placeholder="Password" />
</label>
<label>
Keep me logged in<input type="checkbox" checked />
</label>
<label>
<button type="submit" id="signin_btn"><span>Login</span></button>
</label>
{{ Form::close() }}
JAVASCRIPT:
$(document).ready(function(){
$('#login_form').submit(function(ev){
ev.preventDefault();
$('#signin_btn > span').addClass('btn-loading');
$('#login_errors').html('');
$.ajax({
type:"post",
url:"{{url('/backend/login')}}",
data:$('#login_form').serialize(),
success:function(data){
var result = JSON.parse(data);
$('#signin_btn > span').removeClass('btn-loading');
if(result.result==0){
errors = "<ul>";
result.message.forEach(function(object) {
errors +='<li>'+object+'</li>'
})
errors += "</ul>";
show_response("error","Error",errors)
}else{
if(result.response.type=='success'){
window.location.href = '{{url("backend/dashboard")}}';
}
else
show_response("error","Error","Incorrect email or password")
}
}
});
})
})
PHP:
public function login_submit(Request $request){
$rules = array(
'email' => 'required|email',
'password' => 'required',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
$return['result']=0;
$return['message']=$validator->errors()->all();
echo json_encode($return);
} else {
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::guard('admin')->attempt($credentials)) {
$return['response']['type'] ='success';
$return['response']['message']='Success!';
echo json_encode($return);
}else{
$return['response']['type'] ='error';
$return['response']['message']='Your username or password is incorrect';
echo json_encode($return);
}
}
}
use below code it might help
$(function(){
$('#login_form').on('submit',function(e){
$.ajaxSetup({
header:$('meta[name="_token"]').attr('content')
})
e.preventDefault(e);
$.ajax({
type:"POST",
url:'/register',
data:$(this).serialize(),
dataType: 'json',
success: function(data){
console.log(data);
},
error: function(data){
}
})
});
});
Try this:
{{ Form::open(array('id'=>'login_form','class'=> 'panel p-a-4')) }}
{{ csrf_field() }}
<label>
<input type="email" name="email" placeholder="Email" />
</label>
<label>
<input type="password" name="password" placeholder="Password" />
</label>
<label>
Keep me logged in<input type="checkbox" checked />
</label>
<label>
<button type="submit" id="signin_btn"><span>Login</span></button>
</label>
{{ Form::close() }}
@silverxjohn The form open create the csrf field by itself you don't need to add the
{{ csrf_field() }}
Please or to participate in this conversation.