angularjs - laravel header Allow Origin error while calling api - TagMerge
4laravel header Allow Origin error while calling apilaravel header Allow Origin error while calling api

laravel header Allow Origin error while calling api

Asked 1 years ago
2
4 answers

I added

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

in public/index.php and removed the Cors middleware and all done.

Source: link

1

Probably because Ionic2 itself has not been allowed to make network requests to your endpoint.

You will need to update the Ionic2 app not the server-side (Laravel).

In your Ionic2 config.xml you most likely need to adjust the access attribute, see the documentation here:

https://cordova.apache.org/docs/en/latest/config_ref/#access

Source: link

0

i create a api which return the list of users.i test it in postman app and working fine (return json) . but when i try to call it in ionic2 then it show me error in console.i try to insert header('Access-Control-Allow-Origin: *'); in public/index.php at the top . and also try to make a middleware and the handler() has following code
return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
route/api.php
Route::group(['middleware' => ['Cors']] ,function(){

     Route::get('/nearby',['as' => 'people.nearby' , 'uses' => function(){
       return User::all();
     }]);
});
solved : i add
header('Access-Control-Allow-Origin: *');

Source: link

0

api_routes.php:
<?php

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', ['middleware' => 'cors'], function ($api) {

    $api->post('auth/login', 'App\Api\V1\Controllers\AuthController@login');
    $api->post('auth/signup', 'App\Api\V1\Controllers\AuthController@signup');
    $api->post('auth/recovery', 'App\Api\V1\Controllers\AuthController@recovery');
    $api->post('auth/reset', 'App\Api\V1\Controllers\AuthController@reset');

    // example of protected route
    $api->get('protected', ['middleware' => ['api.auth'], function () {     
        return \App\User::all();
    }]);

    // example of free route
    $api->get('free', function() {
        return \App\User::all();
    });

});
My config/cors.php:
return [
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'],
    'exposedHeaders' => [],
    'maxAge' => 0,
    'hosts' => [],
];
@victor-ponamariov My App working! Because PHP ini bug:
PHP Deprecated:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
Update composer.json to:
"barryvdh/laravel-cors": "0.8.1",
I get the error message:
XMLHttpRequest cannot load http://mmorts.dev/api/v1/login. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, http://client.dev', but only one is allowed. Origin 'http://client.dev' is therefore not allowed access.

Source: link

Recent Questions on angularjs

    Programming Languages