我正在为我的 Laravel 应用程序进行 Ajax 登录。

我正在使用 Angular:

$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'
  }

它仍然无法运作。

expectsJson():

public function expectsJson()
    {
        return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
    }

我的标题:

Accept:application/json,text/plain,/

Accept-Encoding:gzip,放气

Accept-Language:en-US,恩; q=0.8

Connection:keep-alive

Content-Length:57

Content-Type:application/json

X-Requested-With:XMLHttpRequest

出于安全原因,我不包括 tokens/cookies 和原始网址,但它们在那里。

编辑:我尝试清除工匠缓存,但仍然没有。

php artisan cache:clear

composer dump-autoload