title: Laravel 5 Ajax post
body: <p>Hi I am really having a hard time on the new structures in laravel 5, I'm trying to submit a form via AJAX post but I keep getting error 422 (Bad Request). Am I missing something or do I need to do something with my Request class? Here is my code:</p>
  
  <p>Controller:</p>
  
  <pre><code>public function login(LoginRequest $request)
  {
  
      if ($this-&gt;auth-&gt;attempt($request-&gt;only('email', 'password')))
      {
          return redirect("/");
      }
  
      return response()-&gt;json(['errors'=&gt;$request-&gt;response]);
  }
  </code></pre>
  
  <p>LoginRequest file (I added a custom response method which is):</p>
  
  <pre><code>public function response(array $errors)
  {
      if ($this-&gt;ajax() || $this-&gt;wantsJson())
      {
          return response()-&gt;json($errors, 422);
      }
  
      return response()-&gt;json($errors);
  }
  </code></pre>
  
  <p>My ajax code:</p>
  
  <pre><code>$("#form-login").submit(function(){
  
    var selector = $(this);
    $.ajax({
       url: selector.attr("action"),
       type: "post",
       data: selector.serialize(),
       dataType: "json",
    }).done(function(data){
       console.log(data);
       if(data.status == "failed"){
         alert("error");
       }else{
          alert("success");
       }
    });
  
    return false;
  });
  </code></pre>
  
  <p>So My problem is that when I submit my form all I can see from my console is - Failed to load resource: the server responded with a status of 422 (Bad Request)</p>
  
  <p>Please if anyone can help. Thanks in advance!</p>