Laravel $ request-> expectsJson()

Sum*_*per 2 json laravel angularjs laravel-5 laravel-5.4

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

我正在使用Angular:

$http({
  method: 'POST',
  url: '/admin/login',
  headers: {
        'Content-Type': 'application/json'
  },
  data: {email:$scope.email,password:$scope.password}
})
Run Code Online (Sandbox Code Playgroud)

这个请求工作正常,我的问题是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);
    }
Run Code Online (Sandbox Code Playgroud)

那就是Laravel框架代码。它使我一直重定向,但我想触发条件if ($request->expectsJson())并获取JSON响应而不是重定向。

为了触发该条件,我缺少什么?

我什至补充说:

 headers: {
        'Content-Type': 'application/json','X-Requested-With' :'XMLHttpRequest'
  }
Run Code Online (Sandbox Code Playgroud)

而且仍然无法正常工作。

ExpectsJson()

public function expectsJson()
    {
        return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
    }
Run Code Online (Sandbox Code Playgroud)

我的标题:

接受:application / json,text / plain,/

Accept-Encoding:gzip,放气

接受语言:en-US,en; q = 0.8

连接方式:保持活跃

内容长度:57

内容类型:application / json

X-Requested-With:XMLHttpRequest

出于安全原因,我不包括令牌/ cookie和原始URL,但它们都在其中。

编辑:我试图清除工匠缓存,仍然一无所获。

php artisan cache:clear

composer dump-autoload

4gi*_*ivN 5

您不应该使用Accept密钥Content/type。有关更多详细信息,请查看此github讨论

在此处输入图片说明