Error XMLHttpRequest (Ionic + Laravel)

0

Good morning! I'm developing a hybrid application using ionic, when I try to send a post to my server (laravel) the error below occurs:

  

XMLHttpRequest can not load link . In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access.

Here is my service that contains the URL:

angular.module('starter.services')
.factory('Order', ['$resource', 'appConfig',function($resource, appConfig) {
    return $resource(appConfig.baseUrl + '/api/client/order/:id', {id: '@id'},{
        query: {
            isArray: false
        }
    });
}]);

The cors is activated in laravel, it follows my routes where it contains the URL:

Route::group(['middleware' => 'cors'], function(){
Route::post('oauth/access_token', function() {
    return Response::json(Authorizer::issueAccessToken());
});
Route::group(['prefix'=>'api', 'middleware'=>'oauth','as'=>'api.'], function(){
    Route::group(['prefix'=>'client','middleware'=>'oauth.checkrole:client','as'=>'client.'], function(){
        Route::resource('order',
            'Api\Client\ClientCheckoutController', [
                'except' => ['create', 'edit', 'destroy']
            ]);
        Route::get('products','Api\Client\ClientProductController@index');
    });

});

Following file cors.php:

    <?php
return [
    /*
     |--------------------------------------------------------------------------
     | Laravel CORS
     |--------------------------------------------------------------------------
     |
     | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
     | to accept any value.
     |
     */
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE'],
    'exposedHeaders' => [],
    'maxAge' => 0,
    'hosts' => [],
];

Thank you guys.

    
asked by anonymous 15.07.2016 / 14:11

2 answers

1

Speak Eduardo, all good?

The Laravel Cors package actually has a bug! Calm down, it's not you who's going crazy ... We had this problem recently in an application, in the angular coupling with laravel 5.

After searching deeply we found this issue: link

You even have my comment over there: link

We are trying to investigate the fix to send a pull, but for now without success!

The problem is basically in the middleware registry in the kernel. To work, the middleware needs to be registered in the global middlewares, not in the group, strange, is not it?

So the problem with CORS will be solved.

Hugs!

    
21.07.2016 / 13:22
0

I'm not an expert on the subject, but ...

Activation uses the command: php artisan make:middleware Cors Edit the file /app/Http/Middleware/Cors.php to:     

namespace App\Http\Middleware;

use Closure;

class Cors {
    public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

Update the file /app/Http/Kernel.php to:

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'cors' => \App\Http\Middleware\Cors::class, // <<< add this line
];

Example usage:

Route::get('breweries', ['middleware' => 'cors', function()
{
    return \Response::json(\App\Brewery::with('beers', 'geocode')->paginate(10), 200);
}]);

Retrieved from site: link

    
20.07.2016 / 19:03