簡體   English   中英

如何重寫用於AJAX的方法?

[英]How to rewrite a method for using with AJAX?

我有一個特征 ,可以處理密碼恢復邏輯:

public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());

    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $this->resetPassword($user, $password);
        }
    );

    return $response == Password::PASSWORD_RESET
                ? $this->sendResetResponse($response)
                : $this->sendResetFailedResponse($request, $response);
}

protected function rules()
{
    return [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ];
}

protected function sendResetFailedResponse(Request $request, $response)
{
    return redirect()->back()
                ->withInput($request->only('email'))
                ->withErrors(['email' => trans($response)]);
}

我想將其與AJAX調用一起使用。 我應該如何重寫sendResetFailedResponse()
當我在沒有AJAX的情況下使用此邏輯時,如果對rules()驗證失敗,我只會得到帶有422狀態代碼的錯誤響應。 但是,如果在檢查令牌有效性( reset() )時驗證失敗,則返回的狀態碼沒有錯誤。
我的AJAX就像

axios.post('/password/reset', {
                    //data to send
                })
                .then((response) => {
                    ...
                })
                .catch((error) => {
                    //I can catch errors which are returning from rules() fail
                    //I want to catch non-valid token error here too
                }); 

我試圖覆蓋

protected function sendResetFailedResponse(Request $request, $response)
{
    return response(['email' => trans($response)]);
}

但是此代碼在AJAX .catch()之后返回令牌錯誤

我只是在reset方法中執行此操作,效果很好。

public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());
    // Here we will attempt to reset the user's password. If it is successful we
    // will update the password on an actual user model and persist it to the
    // database. Otherwise we will parse the error and return the response.
    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $this->resetPassword($user, $password);
        }
    );
    if ($request->ajax()){
        if ($response == Password::PASSWORD_RESET) {
            return response()->json(['message' => 'Success'],200);
        } else {
            return response()->json(['error' => 'Please try again'], 422);
        }
    }
    // If the password was successfully reset, we will redirect the user back to
    // the application's home authenticated view. If there is an error we can
    // redirect them back to where they came from with their error message.
    return $response == Password::PASSWORD_RESET
    ? $this->sendResetResponse($response)
    : $this->sendResetFailedResponse($request, $response);
}

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM