0

I am making an API and so far the login POST request worked just fine

$http({
  url:'http://myurl/public/login',
  method:"POST",
  data:{usuario:$scope.usuario, password:$scope.password}
})
.then(function(response){
  //exito.

  if (response.data.status=="ok"||response.data.status==ok||response.data.status=='ok'){
    $cookies.put('jwtToken', response.data.token);
    $cookies.put('cliente_id', response.data.cliente_id);
    myNavigator.pushPage('components/inicio/inicio.html');
  }

})
.then(function(response){
  //fallo
  if (response.data.status=="error"||response.data.status==error||response.data.status=='error'){
  ons.notification.alert("Usuario y/o contraseña incorrecta");
}

I have no problem with the login, but when I try to do a GET request

 var cliente_id = $cookies.get('cliente_id');
$http({
 method:"GET",
 url:"http://myurl/public/informacion/"+cliente_id,
 headers:{'Authorization':'Bearer'+' '+ $cookies.get('jwtToken')}
 })
 .then(function(response){
 console.log(response.data);
 })
 .then(function(response){
  console.log("fallo");
  })

I get this error

Failed to load http://myurl/public/informacion/1376: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 500.

And I have this on my server

$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
        ->withHeader('Access-Control-Allow-Origin', '*')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization, token')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  });

I read somewhere that for headers with Authorization I need OPTIONS and I do have it, so if someknows why is this happening to me, please help.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Jaime Cuellar
  • 464
  • 1
  • 5
  • 20

1 Answers1

0

I have also faced same issue for my PHP application so i have added below code in index.php file at top of the file

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');

Hope this will help. Thanks

Binit Ghetiya
  • 1,919
  • 2
  • 21
  • 31