I'm developing an app with Ionic and using Lumen in the backend API. I'm having problems with 'CORS', I already configured the server (both using the internal PHP server and Apache, and the same error occurs in both) creating a middleware for CORS that adds the options to the header as it did not work I disabled this middleware and I installed the middleware available in this project barryvdh / laravel-cors I configured it to run conitnua presenting error making the call to the API through the ionic app. If you do a test using Postman the test is performed normally and you can see that the headers of the header appear as they should. I made several searches and all refer the CORS configuration in the headers of the responses that the server sends. I really do not have any idea where the problem might be. Below are the codes and image of the errors.
ThemiddlewarecodeIcreatedmyself:
namespaceApp\Http\Middleware;useClosure;classCorsMiddleware{/***Handleanincomingrequest.**@param\Illuminate\Http\Request$request*@param\Closure$next*@returnmixed*/publicfunctionhandle($request,Closure$next){return$next($request)->header('Access-Control-Allow-Headers',"Origin, X-Requested-With, Content-Type, Accept")
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Credentials', true);
}
}
As the code above did not work, I disabled this middleware and installed the middleware of the barryvdh / laravel-cors project. In the app.php file I added the lines:
$app->routeMiddleware([
'cors' => \Barryvdh\Cors\HandleCors::class
]);
$app->configure('cors');
$app->register(\Barryvdh\Cors\ServiceProvider::class);
The response header with the middleware that I created by doing the request by Postman is the figure below:
Andusingthemiddlewareofthebarryvdh/laravel-corsprojectisthefigurebelow:
ToendI'llputtheAPIcallasitisintheIonicapp(theionicappiscrawlingunder link ):
avancar(){
this.http.post('http://localhost/api/public/clientes/novo', this.data)
.map(res => res.json())
.subscribe(
res => console.log(res),
error => console.log(error)
);
this.navCtrl.setRoot(ConfirmacaoPage);
}
Thanks to everyone!