[英]How to send login errors back without any redirect ? Laravel 5.4
在Laravel代碼: AuthenticatesUsers.php
有一個函數可以將錯誤消息返回給用戶,例如,如果他們沒有輸入注冊的電子郵件地址:
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}
我的問題是我正在使用“模態”框,因此當此函數重定向到同一頁面時,模態消失,並且用戶僅在單擊以再次查看模態時才能看到錯誤。
如何不使用redirect()或進行任何刷新就將數據發送回去?
這是我正在使用的AJAX調用:
<script type="text/javascript">
$(function(){
$('#login').click(function(e) {
e.preventDefault();
$('#myModal').modal();
});
$(document).on('submit', '#formLogin', function(e) {
e.preventDefault();
$.ajax({
method: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: "json"
})
.done(function(data) {
console.log(data);
})
.fail(function(data) {
console.log(data);
});
});
})
</script>
為了完成,這是完整的Modal,包括注冊和登錄表單:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">recharge.ae | Welcome!</h4>
</div>
<div class="modal-body">
<div id="reg-div">
<h4>New customer? Register below.</h4>
<form class="form-horizontal" method="POST" action="{{ route('register') }}" id="formRegister">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
{{-- <small class="help-block"></small> --}}
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<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-reg" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
{{-- <small class="help-block"></small> --}}
@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" required>
{{-- <small class="help-block"></small> --}}
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register and Log Me In
</button>
</div>
</div>
</form>
</div><!--#reg-div-->
<div id="login-div">
<h4>Returning customer? Login below.</h4>
<form class="form-horizontal" method="POST" action="{{ route('login') }}" id="loginForm">
{{ 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') }}" required autofocus>
@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" required>
@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" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div><!-- #login-div -->
</div>
</div>
</div>
</div>
好吧,返回一個JSON響應而不是重定向頁面。 這是操作方法。 而不是這個
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
寫這個
return json_encode([ 'errorr' => $$errors ]);
通過上面的行,您可以在ajax的done函數中獲取數據。 在那里,您只需要像這樣解析JSON
data = JSON.parse(data);
然后您可以訪問此類data.errors
類的錯誤, 但是在這里您需要記住一件事,您需要自己將這些錯誤附加到html中
PS:如果您有不明白的地方,請隨時詢問
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.