![](/img/trans.png)
[英]How to prevent redirection when using auth()->login() method in laravel?
[英]Laravel timeout on auth login
Laravel 5.3-我的目标是通过ajax将登录表单发送到登录控制器(AuthenticatesUsers特质),并获得响应(json可以),因此我可以设置超时,然后重定向到“仪表板”部分(已认证)。 我需要一些前端的超时时间。 这样可以吗? 如果可以的话,提示就足够了。 提前致谢。
JavaScript示例:
$("#login-form").submit(function (e) {
var url = "/login"; // the script where you handle the form input.
$.ajax({
type: "POST",
cache : false,
url: url,
data: $("#login-form").serialize(), // serializes the form's elements.
success: function ()
{ //Do the timeout part, then redirect
topbar.addClass('success');
form.addClass('goAway');
article.addClass('active');
tries = 0;
},
error: function () {
location.reload();
input.addClass('disabled');
topbar.addClass('error');
}
});});
登录表单是通过帖子发送的,我想由我自己而不是通过控制器进行重定向,而我主要关心的是javascript重定向上的csrf令牌会更改。
编辑:我发现的另一件事是ajax安装程序中包括令牌:
$.ajaxSetup({
headers: {'X-CSRF-TOKEN': _token}
});
并防止表单默认操作:
$("#login-form").submit(function (e) {
e.preventDefault();
这是我的登录表单(所有js和CSS都包含在父视图中):
@extends('layouts.app')
@section('content')
<form class="form form-horizontal" id="login-form" role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
<div class="forceColor"></div>
<div id="logosm_wrapper">
<img id="logosm_login" src="img/ipism_100x50.png" alt="logo" >
</div>
<div class="forceColor"></div>
@if (count($errors))
<div class="topbar error">
@else
<div class="topbar">
@endif
<div class="spanColor"></div>
<input id="email" type="email" class="input form-control" name="email" placeholder="E-mail" value="{{ old('email') }}">
</div>
@if (count($errors))
<div class="topbar error">
@else
<div class="topbar">
@endif
<div class="spanColor"></div>
<input id="password" type="password" class="input form-control" name="password" placeholder="Password"/>
</div>
<button class="submit" id="submit">Login</button>
<!--input class="submit" id="submit" type="submit" value="Login"-->
</form>
@endsection
这按Laravel Auth的预期进行,我的意图是通过JS清除授权上的输入字段,然后将用户重定向到仪表板...
我建议你在你的ajax中添加:
$.ajax({
....
async : false,
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.