我嘗試使用Laravel 5.2 Auth使用Ajax創建登錄表單。使用Laravel使用Ajax登錄表單5.2
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#login').on('click',function(e){
e.preventDefault();
var formData = {
email: $('#email').val(),
password: $('#password').val(),
}
$.ajax({
type: "POST",
url: "/login",
data: formData,
success: function (data) {
location.reload();
},
error: function (data) {
}
});
});
})enter code here
Laravel默認登錄功能:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles && ! $lockedOut) {
$this->incrementLoginAttempts($request);
}
return $this->sendFailedLoginResponse($request);
}
/登錄返回索引頁作爲響應。 我需要關於錯誤消息或成功消息的json響應。據說,改變Laravel的核心功能並不可取。那我怎麼能得到它?
發佈你的PHP代碼以及 –
HTTP ://堆棧溢出.com/questions/32738763/laravel-csrf-token-mismatch-for-ajax-post-request –
add dataType:'json'in ajax if you want to get json response and post your php code –