Laravel angularJS CORS using barryvdh / laravel-cors

It was six hours, and I still do not get a solution for the next problem.

I am trying to get AngularJS to hit my API from different domains. After searching the Internet, I found this package that says it can "add support for CORS (Cross-Origin Resource Sharing) headers in your Laravel application"

I followed all the instructions. Install this and this to make it work, but still no luck. My server always sends me the same error:

XMLHttpRequest cannot load http: //lab.laracon/v1/lists? Id = 123 & password = whatever & username = OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3 . The requested resource does not have an Access-Control-Allow-Origin header. Origin ' http: //lab.angularapi ' is therefore not allowed.

here is my angular code:

var Demo = angular.module( "Demo", [ "ngResource" ] );
Demo.controller(
  "ListController"
  function( $scope, ,$http, $resource ) {

    $http.defaults.useXDomain = true;

     $scope.useResource = function() {
     var Lists = $resource('http://lab.laracon/v1/lists', {
         username: 'OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3',
         password: 'whatever'
     });
     Lists.get({
         id: 1
     }, function(data) {
         alert(data.ok);
     });
   };

  }
);

Here is my barryvdh laravel-cors configuration file:

'defaults' => array(
        'allow_credentials' => false,
        'allow_origin' => array(),
        'allow_headers' => array(),
        'allow_methods' => array(),
        'expose_headers' => array(),
        'max_age' => 0,
    ),

    'paths' => array(
        '^/v1/' => array(
            'allow_origin' => array('*'),
            // 'allow_headers' => array('Content-Type'),
            'allow_headers' => array('*'),
            'allow_methods' => array('POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'),
            'max_age' => 3600,
        ),
    ),

and finally here is the nginx server configuration:

location / {

        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;

        add_header 'Access-Control-Allow-Origin' 'http://lab.angularapi';
         add_header 'Access-Control-Allow-Credentials' 'false';
         add_header 'Access-Control-Allow-Headers' '*';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';

    }

Can anyone help me? what is wrong with my code and configuration? thank

+4
source share
2 answers

Finally, I found the right solution for my situation:

  • I completely get rid of barryvdh / laravel-cors
  • Thanks for Dan Horrigan for his tweet.

CORS laravel

( , $response- > headers- > set() . :

public function __construct()
    {
        $this->afterFilter(function(){

            header('Access-Control-Allow-Origin: *');

        });
    }

:)

+4

laravel-cors, . Access-Control-Allow-Origin: * ( ) , , ..

Laravel .

Route::filter('access-control', function($route, $request, $response)
{
    $response->headers->set('Access-Control-Allow-Origin', '*');
});

afterFilter routes.php.

public function __construct()
{
    $this->afterFilter('access-control');
}

, :

Route::when('api/*', 'access-control');

header() . , Nginx, Laravel, .

+2

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


All Articles