I am trying to create a simple Angular application. I created an API in Laravel that has only two simple routes that send back airport and json flight data. I configured the Angular application to retrieve data using a service like this:
services.js
angular.module('airlineServices', ['ngResource'])
.factory('Airport', function($resource){
return $resource("http://angulair.rohanchhabra.in/airports/:id", {}, {
query: { method: "GET", isArray: false }
});
});
Now, when I use the resource as a simple json file located in the Angular directory itself, it works. But when I try to retrieve data from the Laravel API from my service, as in the above code, I see an error in the console saying:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://angulair.rohanchhabra.in/airports. This can be fixed by moving the resource to the same domain or enabling CORS.
I installed the https://github.com/barryvdh/laravel-cors package , which should enable CORS. The configuration file looks like this:
<?php
return array(
'defaults' => array(
'supportsCredentials' => false,
'allowedOrigins' => array('*'),
'allowedHeaders' => array('*'),
'allowedMethods' => array('*'),
'exposedHeaders' => array(),
'maxAge' => 0,
'hosts' => array(),
),
'paths' => array(
'api/*' => array(
'allowedOrigins' => array('*'),
'allowedHeaders' => array('*'),
'allowedMethods' => array('*'),
'maxAge' => 3600,
),
'*' => array(
'allowedOrigins' => array('*'),
'allowedHeaders' => array('Content-Type'),
'allowedMethods' => array('POST', 'PUT', 'GET', 'DELETE'),
'maxAge' => 3600,
'hosts' => array('api.*'),
),
),
);
I even tried adding code in App: after () to filters.php, for example:
App::after(function($request, $response)
{
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
});
. .htaccess :
Header set Access-Control-Allow-Origin "*"
.
, , , , :
API Laravel
Angular
? CORS, API ?