简体   繁体   English

如何在没有任何重定向的情况下发回登录错误? Laravel 5.4

[英]How to send login errors back without any redirect ? Laravel 5.4

In the Laravel code: AuthenticatesUsers.php there's this function that returns error messages back to the user, for example if they didn't enter a registered email address: 在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);
}

My issue is that I'm using a Modal box so when this function redirects to the same page the modal disappears and the user can only see the errors when he clicks to see the modal again. 我的问题是我正在使用“模态”框,因此当此函数重定向到同一页面时,模态消失,并且用户在单击以再次查看模态时才能看到错误。

How can I send the data back without using redirect() or any refresh ? 如何不使用redirect()或进行任何刷新就将数据发送回去?

This is the AJAX call I'm using: 这是我正在使用的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>

For completion, this is the entire Modal with both the registration and login forms: 为了完成,这是完整的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>

Well, return a JSON response rather than redirecting the page. 好吧,返回一个JSON响应而不是重定向页面。 Here's how to do it. 这是操作方法。 Rather than this 而不是这个

return redirect()->back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors($errors);

write this 写这个

return json_encode([ 'errorr' => $$errors ]);

and via above line, you get the data in the done function of the ajax. 通过上面的行,您可以在ajax的done函数中获取数据。 There you just need to parse that JSON like this 在那里,您只需要像这样解析JSON

data = JSON.parse(data);

and then you can access the errors like this data.errors but here you need to keep one thing in mind you will need to attach these errors in the html yourself 然后您可以访问此类data.errors类的错误, 但是在这里您需要记住一件事,您需要自己将这些错误附加到html中

PS If there is anything that you don't understand feel free to ask PS:如果您有不明白的地方,请随时询问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM