vendredi 14 octobre 2016

Chrome extension cross origin with laravel

I'm creating a chrome extension to communicate with my laravel app.

I've configured laravel cors on my project.

I added this line on my app.php file :

Barryvdh\Cors\ServiceProvider::class,

I added cors middleware to my route group :

Route::group(['prefix' => 'api/', 'middleware' => ['cors']], function () {
    // routes ...
});

In my chrome extension I call my route with $http :

$http({
   url: 'http://localhost:8000/api/bookmarks',
   method: 'GET'
}).then(function (data) {
    console.log(data);
}).catch(function (err) {
        console.log(err);
})

In chrome debugger I've following error :

XMLHttpRequest cannot load http://localhost:8000/api/v1/bookmarks. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.   Origin 
'chrome-extension://ndgoclipaencbojjmcfkohfekdbhdnjf' is therefore not allowed access.

I tried many solution found on github or stackoverflow :

Add this on index.php header("Access-Control-Allow-Origin: *");

Change defaults header config with angular :

app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.headers.common = 'Content-Type: application/json';
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

Change $execpt property on VerifyCsrfToken class :

protected $except = [
    'auth/*', 'api/*'
];

I have missed something in my config ?

Thanks for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire