Laravel Api Rest Doesn't Work In Device Android
I have an app in IONIC and in browser the call to API works but when I run on android device it shows this error: HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: 'U
Solution 1:
The issue is related to CORS. You don't have to do anything to your IONIC app. You can enable CORS request by adding required headers for that you can create your own middleware in Laravel to handle cors, A sample middleware would be:
namespaceApp\Http\Middleware;
useClosure;
classCors{
publicfunctionhandle($request, Closure$next)
{
return$next($request)->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers', '*');
}
}
Then use it, by editing app\Http\Kernel.php
protected$middlewareGroups = [
'web' => [
// middleware for your web routes
],
'api' => [
'throttle:60,1',
'bindings',
'cors',
],
]
protected$routeMiddleware = [
// other middleware code'cors' => \EuroKids\Http\Middleware\Cors::class,
]
You can customize the above middleware as required.
However, if you don't want to do create your own middleware you can use this library: https://github.com/barryvdh/laravel-cors
Post a Comment for "Laravel Api Rest Doesn't Work In Device Android"