在我的项目中,我有一个页面来处理登录表单,注册表单和密码重置表单。
对于前两个,一切都运作良好。最后,我找到了一种方法来发送我自己的电子邮件而不是laravel的默认电子邮件,但现在我想停止重定向成功。
对于这个故事,当我点击提交,用Ajax调用重置控制器并等待响应时,我发出了一个sweetalert通知。
但相反,页面会在成功时重新加载。我找不到防止重定向的方法,只是将json发送到我的Ajax。
我的login.js:
$(".m_login_forget_password_submit").click(function(l){
var t=$(this),r=$(this).closest("form");
t.addClass("m-loader m-loader--right m-loader--light");
swal({
title:"Mot de passe",
text:"Génération du lien.",
type:"info"
});
var $this = $(this);
$.ajax({
type: 'POST',
url: 'password/request',
data: $this.serializeArray(),
success:function(data){
$("#m_login_signup_submit").toggleClass("m-loader m-loader--right m-loader--light");
swal({
title:"Succès",
text:"Votre demande de compte a été transmise à nos équipe.",
type:"success"
});
},
error: function(data){
$("#m_login_signup_submit").toggleClass("m-loader m-loader--right m-loader--light");
if( data.status === 422 ) {
var error = "Les erreurs suivantes ont été trouvées :";
var errors = data.responseJSON;
$.each( errors, function( key, value ) {
error += '<br>▸ ' + value[0];
});
swal("Erreur",error,"error")
}
}
});
我的自定义ResetPasswordController:
class ResetPasswordController extends Controller
{
use ResetsPasswords;
/**
* Where to redirect users after resetting their password.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get the response for a successful password reset.
*
* @param string $response
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function sendResetResponse($response)
{
return 'ok';
}
}
有人知道如何防止重定向发送电子邮件重置密码成功并发送json代替?
谢谢您帮忙。
重置密码时,将在reset
中调用ResetPasswordController
函数,因此您必须将sendResetResponse
函数重命名为reset
(因为您要覆盖特征函数)并将默认特征函数指向未使用的函数。另外,如果您需要JSON响应,则需要使用返回指定。
例:
class ResetPasswordController extends Controller
{
use ResetsPasswords {
reset as unused;
}
...
//Override the trait function
public function reset(Request $request) {
//Call default validation and handle password change
$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);
}
);
//Return the response
if ($response == Password::PASSWORD_RESET) {
return response()->json(array(
'result' => 'ok',
), 200);
} else {
return response()->json(array(
'result' => 'fail',
), 500);
}
}
}
编辑:
对不起,以为你在谈论重置密码重定向。要在发送电子邮件后处理重定向,您将采用类似的方法并修改您的ForgotPasswordController
并覆盖sendResetLinkEmail
函数:
class ForgotPasswordController extends Controller
{
use SendsPasswordResetEmails {
sendResetLinkEmail as unused;
}
...
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);
$response = $this->broker()->sendResetLink(
$request->only('email')
);
//Return the response
if ($response == Password::RESET_LINK_SENT) {
return response()->json(array(
'result' => 'ok',
), 200);
} else {
return response()->json(array(
'result' => 'fail',
), 500);
}
}
}