Soumaya Hachem
asked on
cannot use 'in' operator to search for 'length' in ajax Laravel
I want to display message with ajax :
but i have this error in the console :
Cannot use 'in' operator to search for 'length' in L'email ou le mot de passe est incorrect.
Function Ajax
the view code:
but i have this error in the console :
Cannot use 'in' operator to search for 'length' in L'email ou le mot de passe est incorrect.
Function Ajax
<script type="text/javascript">
$(document).ready(function() {
$(".login-btn").click(function(e){
e.preventDefault();
var _token = $("input[name='_token']").val();
var email = $("input[name='email']").val();
var password = $("input[name='password']").val();
$.ajax({
url: "/login",
type:'POST',
cache: false,
data: {_token:_token,email:email, password:password},
dataType: "json",
success: function(data) {
if($.isEmptyObject(data.error)){
window.location = 'home';
}else{
// $(".print-error-msg").find("ul").html('');
// $(".print-error-msg").css('display','block');
// $.each( data, function( key, value ) {
// $(".print-error-msg").find("ul").append('<li>'+data.error+'</li>+');
// });
printErrorMsg (data.error);
}
},
error: function(response){
$(".print-error-msg").find("ul").html('');
$(".print-error-msg").css('display','block');
$(".print-error-msg").find("ul").append('<li>'+response.responseJSON.error+'</li>');
}
});
});
function printErrorMsg (msg) {
$(".print-error-msg").find("ul").html('');
$(".print-error-msg").css('display','block');
$.each( msg, function( key, value ) {
$(".print-error-msg").find("ul").append('<li>'+value+'</li>');
});
}
});
</script>
Code de function laravel in the controller public function loginPost(Request $request){
$rules = array(
'email' => 'required|email|exists:users',
'password' => 'required');
$validator = Validator::make(Input::all() , $rules);
if ($validator->fails())
{
return response()->json(['error'=>$validator->errors()->all()]);
}
else
{
$userdata = array(
'email' => Input::get('email') ,
'password' => Input::get('password')
);
if (Auth::attempt(['email' => Request::get('email'), 'password' => Request::get('password')]))
{
return redirect()->intended();
}
else
{
return response()->json(['error'=>'L\'email ou le mot de passe est incorrect.'], 200);
}
}
}
the view code:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<!-- <div class="alert-danger" style="display:none">
</div> -->
<div class="alert alert-danger print-error-msg" style="display:none">
<ul>
<li></li>
</ul>
</div>
<form class="form-horizontal" role="form" id="btn-login" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary login-btn">
<i class="fa fa-btn fa-sign-in"></i> Login
</button>
<a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
What line of which code is that error referring to - I don't see where you are using IN or where you are using length - can you elaborate.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.