[英]Laravel $request->expectsJson()
我正在為我的 Laravel 應用程序進行 Ajax 登錄。
我正在使用角度:
$http({
method: 'POST',
url: '/admin/login',
headers: {
'Content-Type': 'application/json'
},
data: {email:$scope.email,password:$scope.password}
})
這個請求工作正常,我的問題是 Laravel 的響應總是重定向我,如果我不發送 JSON,它應該:
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}
那就是 Laravel 框架代碼。 它一直將我重定向回來,但我想觸發條件if ($request->expectsJson())
並獲得 JSON 響應而不是重定向。
為了觸發該條件,我缺少什么?
我什至補充說:
headers: {
'Content-Type': 'application/json','X-Requested-With' :'XMLHttpRequest'
}
而且它仍然行不通。
public function expectsJson()
{
return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
}
我的標題:
接受:應用程序/json,文本/純文本, /
接受編碼:gzip、deflate
接受語言:en-US,en;q=0.8
連接:保持活動狀態
內容長度:57
內容類型:應用程序/json
X-Requested-With:XMLHttpRequest
出於安全原因,我沒有包含令牌/cookie 和原始 url,但它們在那里。
編輯:我嘗試清除工匠緩存但仍然沒有。
php artisan cache:clear
還
composer dump-autoload
您應該使用Accept
鍵而不是Content/type
。 有關詳細信息,請查看此 github 討論
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.