5
Laravel 5.3 - 我的目标是通过ajax发送登录表单到登录控制器(AuthenticatesUsers trait),并获得响应(json会好的),所以我可以在重定向到“仪表板“部分(已认证)。我需要一些前端的超时。 那么可以这样做?如果可以的话,暗示就足够了。 在此先感谢。Laravel身份验证登录超时
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验证主编,我的目的是要通过对JS授权绿了输入字段,然后重定向用户仪表盘......
你有没有看https://laracasts.com/discuss/channels/general-discussion/laravel-5-ajax-tokenmismatchexception –
是的,我查了它,这不是我主要关心的,因为你可以关闭令牌验证(获取摆脱)。问题是你可以在验证用户后自己重定向auth与所有数据 –
为什么如果csrf_token在重定向后更改,这是一个问题? –