我在laravel中使用注册和登录表单制作应用程序。我有ajax登录表单。当用户输入正确的邮件并通过时,它可以正常工作,但如果用户输入错误的邮件或在登录页面上重定向它。任何人都可以帮我如何在没有重定向的情况下在登录表单中显示错误?
public function login(){
$data = Request::all();
if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']]))
{
return redirect('/');
}
else {
return redirect('login');
}
}
这是我的控制器代码
答案 0 :(得分:2)
在else部分而不是
return redirect('login');
使用此
Session::flash('errorMessage', 'Authentication Failed');
return Redirect::back();
在视图(刀片)中添加此
@if ($message = Session::get('errorMessage'))
<div class="alert alert-danger alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>Authentication Error</h4>
<span class="errorText">{{ $message }}</span>
</div>
@endif
(不刷新)试试这个: 在else部分添加
return Response::json([
'message' => 'Authentication Failed'
]);
在Ajax成功函数中,您可以从返回JSON中显示此消息。 将消息放在带有“errorText”类的范围中。
在视图中:
<div class="alert alert-danger alert-block hidden">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>Authentication Error</h4>
<span class="errorText"></span>
</div>
请记住使用jquery remove()删除“隐藏”类。
注意:如果需要,请记住包括命名空间Illuminate \ Http \ Request和Illuminate \ Http \ Response。
希望这有用。