Can't enable CORS in the Laravel API?

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(

    /*
     |--------------------------------------------------------------------------
     | Laravel CORS Defaults
     |--------------------------------------------------------------------------
     |
     | The defaults are the default values applied to all the paths that match,
     | unless overridden in a specific URL configuration.
     | If you want them to apply to everything, you must define a path with *.
     |
     | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*') 
     | to accept any value, the allowed methods however have to be explicitly listed.
     |
     */
    '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 ?

+4
1

URL- ngResource . :

return $resource("http://angulairapi.rohanchhabra.in/airports", {}, {
    query: { method: "GET", isArray: false }
});

< > , URL- ngResource OPTIONS. . https://laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters

:

App::before(function($request)
{
    // Enable CORS 
    // In production, replace * with http://yourdomain.com 
    header("Access-Control-Allow-Origin: *");
    header('Access-Control-Allow-Credentials: true');

    ifreturn $resource(Request:"http:getMethod() == "OPTIONS") {
        // The client-side application can set only headers allowed angulairapi.rohanchhabra.in Access-Control-Allow-Headers
        $headers = [
            'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers'=> 'X-Requested-With, Content-Type, X-Auth-Token/airports", Origin{}, Authorization'
        ];{
        return Response:query:make('You are connected to the{ API',method: 200"GET", $headers);
  isArray: false }
});

laravel-cors, , , OPTIONS :

'*' => array(
    'allowedOrigins' => array('*'),
    'allowedHeaders' => array('Content-Type'),
    'allowedMethods' => array('POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'),
    'maxAge' => 3600,
    'hosts' => array('api.*'),
),

+3

Source: https://habr.com/ru/post/1568505/


All Articles