title: Laravel $request->expectsJson()
body: <p>I am doing an Ajax login for my Laravel application.</p>
  <p>I am using Angular:</p>
  <pre><code>$http({
    method: 'POST',
    url: '/admin/login',
    headers: {
          'Content-Type': 'application/json'
    },
    data: {email:$scope.email,password:$scope.password}
  })
  </code></pre>
  <p>This request works fine, my problem is that Laravel's response always redirects me, as it should <em>if I wasn't sending JSON:</em></p>
  <pre><code>protected function sendFailedLoginResponse(Request $request)
      {
          $errors = [$this-&gt;username() =&gt; trans('auth.failed')];
          if ($request-&gt;expectsJson()) {
              return response()-&gt;json($errors, 422);
          }
          return redirect()-&gt;back()
              -&gt;withInput($request-&gt;only($this-&gt;username(), 'remember'))
              -&gt;withErrors($errors);
      }
  </code></pre>
  <p>That is the Laravel framework code. It keeps redirected me back, but I want to fire the conditional <code>if ($request-&gt;expectsJson())</code> and get a JSON response rather than a redirect.</p>
  <p>What am I missing in order to trigger that conditional?</p>
  <p>I even added:</p>
  <pre><code> headers: {
          'Content-Type': 'application/json','X-Requested-With' :'XMLHttpRequest'
    }
  </code></pre>
  <p>And it still won't work.</p>
  <p><a href="https://github.com/laravel/framework/blob/5.3/src/Illuminate/Http/Request.php#L704" rel="noreferrer">expectsJson()</a>:</p>
  <pre><code>public function expectsJson()
      {
          return ($this-&gt;ajax() &amp;&amp; ! $this-&gt;pjax()) || $this-&gt;wantsJson();
      }
  </code></pre>
  <p>My headers:</p>
  <blockquote>
  <p>Accept:application/json, text/plain, <em>/</em></p>
  <p>Accept-Encoding:gzip, deflate</p>
  <p>Accept-Language:en-US,en;q=0.8</p>
  <p>Connection:keep-alive</p>
  <p>Content-Length:57</p>
  <p>Content-Type:application/json</p>
  <p>X-Requested-With:XMLHttpRequest</p>
  </blockquote>
  <p>I'm not included tokens/cookies and the origin url for security reason, but they are in there.</p>
  <p>Edit: I tried clearing the artisan cache and still nothing.</p>
  <p><code>php artisan cache:clear</code></p>
  <p>Also</p>
  <p><code>composer dump-autoload</code></p>