title: Laravel controller returning index view instead of json response
body: <p>I am using apache2 and php artisan serve command to test my project.</p>
  <p>Have the next controller:</p>
  <pre><code>&lt;?php
  // file: Http\Controllers\AuthCtrl.php
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\User; //  &lt;--
  use Illuminate\Support\Facades\Auth;
  
  class AuthCtrl extends Controller
  {
      public function login(Request $request)
      {
          $rules = [ 'email' =&gt; 'required|exists:users',
                     'password'  =&gt; 'required' ];
  
          $request-&gt;validate($rules);
  
          $data = [ 'email' =&gt; $request-&gt;get('email'),
                    'password'  =&gt;  $request-&gt;get('password') ];
  
          if( Auth::attempt($data) )
          {
              $user = Auth::user();
              // the $user-&gt;createToken('appName')-&gt;accessToken generates the JWT token that we can use
              return response()-&gt;json([
                                        'user'  =&gt;  $user, // &lt;- we're sending the user info for frontend usage
                                        'token' =&gt;  $user-&gt;createToken('teltask')-&gt;accessToken, // &lt;- token is generated and sent back to the front end
                                        'success' =&gt; true
                                      ]);
          }
          else
          {
              return response()-&gt;json([
                                      'success' =&gt; false,
                                      'msg' =&gt; 'Credenciales erroneas.'
                                      ]);
          }
  
      }
  }
  </code></pre>
  <p>And a simple mock index.php that does an ajax call with the following js:</p>
  <pre><code>function ExecLogin()
  {
      $.ajax({
          method: &quot;POST&quot;,
          url: &quot;http://127.0.0.1:8000/api/login&quot;,
          data : {email : &quot;fsd@gmail.com&quot;, password: &quot;mypass&quot;}
      }).done(
      function(data, status) 
      {
          if(data.success == true)
          {
              localStorage.setItem('appname_token', data.token);
              // the following part makes sure that all the requests made later with jqXHR will automatically have this header.
              $( document ).ajaxSend(function( event, jqxhr, settings ) {
                  jqxhr.setRequestHeader('Authorization', &quot;Bearer &quot; + data.token); 
              });
          }
          else
          {
              alert(data.msg);
          }
      }).fail(function(error){
          // handle the error
      });
  } 
  </code></pre>
  <p>As you can see, when the code enters this part, (when the user authenticated OK), It returns a JSON response allright:</p>
  <pre><code>$user = Auth::user();
              // the $user-&gt;createToken('appName')-&gt;accessToken generates the JWT token that we can use
              return response()-&gt;json([
                                        'user'  =&gt;  $user, // &lt;- we're sending the user info for frontend usage
                                        'token' =&gt;  $user-&gt;createToken('teltask')-&gt;accessToken, // &lt;- token is generated and sent back to the front end
                                        'success' =&gt; true
                                      ]);
  </code></pre>
  <p>But when it did not authenticated OK, it enters at this part, but the response is the index.php text/html, not a json response as I defined:</p>
  <pre><code>return response()-&gt;json([
                            'success' =&gt; false,
                            'msg' =&gt; 'Credenciales erroneas.'
                                      ]);
  </code></pre>
  <p>I am recieving the following string on the data object of the ajax call:</p>
  <pre><code>&quot;&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
      &lt;head&gt;
          &lt;meta charset=&quot;utf-8&quot;&gt;
          &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  
          &lt;title&gt;Laravel&lt;/title&gt;
  
          &lt;!-- Fonts --&gt;
          &lt;link href=&quot;https://fonts.googleapis.com/css?family=Nunito:200,600&quot; rel=&quot;stylesheet&quot;&gt;
  
          &lt;!-- Styles --&gt;
          &lt;style&gt;
              html, body {
                  background-color: #fff;
                  color: #636b6f;
                  font-family: 'Nunito', sans-serif;
                  font-weight: 200;
                  height: 100vh;
                  margin: 0;
              }
  
              .full-height {
                  height: 100vh;
              }
  
              .flex-center {
                  align-items: center;
                  display: flex;
                  justify-content: center;
              }
  
              .position-ref {
                  position: relative;
              }
  
              .top-right {
                  position: absolute;
                  right: 10px;
                  top: 18px;
              }
  
              .content {
                  text-align: center;
              }
  
              .title {
                  font-size: 84px;
              }
  
              .links &gt; a {
                  color: #636b6f;
                  padding: 0 25px;
                  font-size: 13px;
                  font-weight: 600;
                  letter-spacing: .1rem;
                  text-decoration: none;
                  text-transform: uppercase;
              }
  
              .m-b-md {
                  margin-bottom: 30px;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div class=&quot;flex-center position-ref full-height&quot;&gt;
                              &lt;div class=&quot;top-right links&quot;&gt;
                                              &lt;a href=&quot;http://127.0.0.1:8000/login&quot;&gt;Login&lt;/a&gt;
  
                                                      &lt;a href=&quot;http://127.0.0.1:8000/register&quot;&gt;Register&lt;/a&gt;
                                                              &lt;/div&gt;
              
              &lt;div class=&quot;content&quot;&gt;
                  &lt;div class=&quot;title m-b-md&quot;&gt;
                      Laravel
                  &lt;/div&gt;
  
                  &lt;div class=&quot;links&quot;&gt;
                      &lt;a href=&quot;https://laravel.com/docs&quot;&gt;Docs&lt;/a&gt;
                      &lt;a href=&quot;https://laracasts.com&quot;&gt;Laracasts&lt;/a&gt;
                      &lt;a href=&quot;https://laravel-news.com&quot;&gt;News&lt;/a&gt;
                      &lt;a href=&quot;https://blog.laravel.com&quot;&gt;Blog&lt;/a&gt;
                      &lt;a href=&quot;https://nova.laravel.com&quot;&gt;Nova&lt;/a&gt;
                      &lt;a href=&quot;https://forge.laravel.com&quot;&gt;Forge&lt;/a&gt;
                      &lt;a href=&quot;https://vapor.laravel.com&quot;&gt;Vapor&lt;/a&gt;
                      &lt;a href=&quot;https://github.com/laravel/laravel&quot;&gt;GitHub&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  &quot;
  </code></pre>
  <p>Am I missing something important here?</p>
  <p>Authenticate Middleware looks like this:</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Illuminate\Auth\Middleware\Authenticate as Middleware;
  
  class Authenticate extends Middleware
  {
      /**
       * Get the path the user should be redirected to when they are not authenticated.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return string|null
       */
      protected function redirectTo($request)
      {
          if (! $request-&gt;expectsJson() ) {
              return route('login');
          }
      }
  }
  </code></pre>