I am using Laravel 5 and a library called CORS. I am facing an error that I don't really understand what it is and why it is happening.
I am developing an API that is gonna be used by a website's front-end and an app.
I am trying to do an ajax call to the API:
$.ajax({
url: 'http://myapi.com/call',
type: 'GET',
headers: {'Authorization' : 'Bearer token123'},
crossDomain: true,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success : function(data) { console.log(data);},
error: function(xhr) {console.log(xhr)}
});
However I got this errro:
XMLHttpRequest cannot load http://myapi.com/call. Response for preflight has invalid HTTP status code 500
However, when I remove the following line from the request works fine.
headers: {'Authorization' : 'Bearer token123'},
EDIT: my config/cors.php
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
'exposedHeaders' => [],
'maxAge' => 0,
'hosts' => [],
Any idea about how to solve it?
The following fix worked for me (Laravel 5.4) You can add the following to the laravel cors middleware
and for each of your routes which gets preflight requests add a options method too
Did you configure CORS on server in laravel, there are couple of option to do the same
Source: https://laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters/?page=2