【问题标题】:Laravel password reset with AJAX (AngularJS)使用 AJAX (AngularJS) 重置 Laravel 密码
【发布时间】:2019-03-25 09:57:35
【问题描述】:

我正在使用 Laravel 5.7 和 angularjs 构建应用程序。我想通过ajax进行授权。登录和注册工作正常,但重置密码工作不正确。问题是没有错误或成功回调消息。我注意到我的请求转到了正确的路线,但随后它将我重定向到了我的主页。所以在回应中,我总是得到我的索引页。

默认情况下,laravel 会抛出错误“我们找不到具有该电子邮件地址的用户”。但是如果你使用ajax,它不会抛出任何东西,我也不知道问题出在哪里,因为登录和注册都会成功抛出错误。

//HTML template
<form ng-submit="auth('{{ route('password.email') }}')">
    @csrf
    <div>
        <div>Enter your e-mail</div>
        <div>We will send you an email with a link to create a new password.
        </div>
    <div ng-repeat="error in authErrors.errors">@{{error[0]}}</div>
    </div>
    <div>
        <label for="email">{{ __('E-Mail Address') }}</label>
        <input name="email" ng-model="authInfo.email" type="email" required>
    </div>
    <button type="submit" name="submit">{{ __('Send Password Reset Link') }}</button>
</form>
//JS
    $scope.authInfo = {
        name: null,
        email: null,
        password: null,
        passwordConfirmation: null,
        remember: false
    }
    $scope.authErrors = {};

    $scope.auth = function(route){
        $http({
            method: 'POST',
            url: route,
            data: $scope.authInfo
        }).then(function(response) {
            console.log($scope.authInfo);
            console.log(response);
        }, function(response) {
            console.log($scope.authInfo);
            $scope.authErrors = response.data;
            console.log(response.data)
        })
    }

身份验证控制器都是默认的。
我也在 clear 项目上测试了 ajax 并得到了相同的结果。 如果我不发送电子邮件作为数据,它会抛出一个错误,即没有电子邮件。但如果我发送一封电子邮件,我会得到重定向。
我希望获得与注册和登录功能相同的结果。

有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: angularjs laravel laravel-5


    【解决方案1】:

    它的行为符合预期,因为此函数在成功和失败情况下都会返回 html。你需要做的是覆盖下面的函数 -

    public function sendResetLinkEmail(Request $request)
    {
    
        if($request->ajax()) {
    
            $this->validateEmail($request);
    
            // We will send the password reset link to this user. Once we have attempted
            // to send the link, we will examine the response then see the message we
            // need to show to the user. Finally, we'll send out a proper response.
            $response = $this->broker()->sendResetLink(
                $request->only('email')
            );
    
            return $response == Password::RESET_LINK_SENT
                ? [
                    'msg' => 'Successful'
                ]
                : [
                    'msg' => 'Reset email sending failed.'
                ];
        }
    
        return [
            'msg' => 'Un-expected method calls'
        ];
    }
    

    把函数放到ForgotPasswordController

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 2015-05-27
      • 2017-08-04
      • 2020-10-01
      • 1970-01-01
      相关资源
      最近更新 更多