title: Response to preflight request doesn't pass access control check Laravel and Ajax call
body: <p>I have a REST api made in Laravel 5.1 hosted in a remote server. Now, I', trying to consume that API from another website (that I have in local).</p>
  
  <p>In Laravel I set the required lines to send the CORS headers. I also tested the API using Postman and everything seems to be ok!</p>
  
  <p><strong>In the Frontend</strong></p>
  
  <p>Then, in the website I sent the POST request using ajax, with this code:</p>
  
  <pre><code>var url="http://xxx.xxx.xxx.xxx/apiLocation";
  var data=$("#my-form").serialize();
      $.ajax({
                      type: "POST",
                      url: url,
                      data: data,
                      headers: { 'token': 'someAPItoken that I need to send'},
                      success: function(data) {
                          console.log(data);
                      },
                      dataType: "json",
                  }); 
  </code></pre>
  
  <p>Buy then I get this error in the console:</p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load <a href="http://xxx.xxx.xxx.xxx/apiLocation" rel="noreferrer">http://xxx.xxx.xxx.xxx/apiLocation</a>.
    Response to preflight request doesn't pass access control check: No
    'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin '<a href="http://localhost" rel="noreferrer">http://localhost</a>' is therefore not allowed access.</p>
  </blockquote>
  
  <p><strong>In the Backend</strong></p>
  
  <p>In the API I set this (using a Laravel Middleware to set the headers):</p>
  
  <pre><code>return $next($request)
              -&gt;header('Access-Control-Allow-Origin', '*')
              -&gt;header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  </code></pre>
  
  <p>So, I'm confused about where is exactly the problem. </p>
  
  <ol>
  <li>In the server? but then why with Postman work fine?</li>
  <li>Is in the Ajax call? so, then what should I add?</li>
  </ol>