gpt4 book ai didi

jquery - Laravel 5.3 Ajax 登录

转载 作者:行者123 更新时间:2023-12-01 00:05:35 25 4
gpt4 key购买 nike

我正在尝试使用新的 Laravel 5.3 项目使用 ajax 登录我的用户。

我已经生成了身份验证路由,并将其添加到我的 web.php 中:

Auth::routes();

我有一个包含电子邮件、密码输入和 csrf 字段的 html 表单。然后我还有这个 javascript 文件:

$("form.login").submit(function(e) {
e.preventDefault();

$.ajax({
method: "POST",
dataType: "json",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
data: $("form.login").serialize(),
url: "/login"
})
.done(function(data) {
console.log(data);
});
});

但是,当我发布它时,它会显示在我的网络选项卡中: chrome dev tools

它重定向回原始页面,不返回任何数据。

为什么要这样做? 5.3 不再提供 json 响应了吗?

最佳答案

完整的解决方案:

嗨,赖尼尔科斯,

我也尝试对5.3版本做同样的事情,我终于解决了它:)并且解决方案非常干净。

首先,我在App\Http\Controllers\Api 创建了一个名为 Auth的新文件夹,我这样做只是为了添加新的身份验证api 的 Controller ,以便我可以重写一些函数,然后我将身份验证 Controller ( LoginControllerForgotPasswordControllerRegisterController)复制到这个新文件夹。

在 LoginController 类中:我重写了进行重定向的函数。

The first function: will be automatically called when theauthentication return success.

The second function: will be automatically called when theauthentication return error.

The last function: will be automatically called when the user has beenlocked out after trying 5 login attempts.

     /**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request) {
$this->clearLoginAttempts($request);

return response()->json(['SUCCESS' => 'AUTHENTICATED'], 200);
}

/**
* Get the failed login response instance.
*
* @return \Illuminate\Http\Response
*/
protected function sendFailedLoginResponse() {
return response()->json(['ERROR' => 'AUTH_FAILED'], 401);
}

/**
* Error after determining they are locked out.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLockoutResponse(Request $request) {
$seconds = $this->limiter()->availableIn(
$this->throttleKey($request)
);

return response()->json(['ERROR' => 'TOO_MANY_ATTEMPTS', 'WAIT' => $seconds], 401);
}

在 RegisterController 类中:我重写了进行重定向的函数。

In the first function: I modified the validator response to return a more comfortable response (array) to work with.

The second function: will be automatically called when the registration return success.

    /**
* Handle a registration request for the application.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request) {
$validator = $this->validator($request->all());

if($validator->fails())
return response()->json(['ERROR' => $validator->errors()->getMessages()], 422);

event(new Registered($user = $this->create($request->all())));

$this->guard()->login($user);

return $this->registered($request, $user)
?: redirect($this->redirectPath());
}

/**
* The user has been registered.
*
* @param Request $request
* @param mixed $user
* @return mixed
*/
protected function registered(Request $request, $user) {
return response()->json(['SUCCESS' => 'AUTHENTICATED']);
}

在 ForgotPasswordController 类中:我重写了进行重定向的函数。

I modified the reset link email function so we can get the messages and display as json instead of the redirects.

     /**
* Send a reset link to the given user.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function sendResetLinkEmail(Request $request)
{
$validator = Validator::make($request->only('email'), [
'email' => 'required|email',
]);

if ($validator->fails())
return response()->json(['ERROR' => 'VALID_EMAIL_REQUIRED'], 422);

// 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')
);

if ($response === Password::RESET_LINK_SENT) {
return response()->json(['SUCCESS' => 'EMAIL_SENT'], 200);
}

// If an error was returned by the password broker, we will get this message
// translated so we can notify a user of the problem. We'll redirect back
// to where the users came from so they can attempt this process again.
return response()->json(['ERROR' => 'EMAIL_NOT_FOUND'], 401);
}

关于jquery - Laravel 5.3 Ajax 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39675978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com