"No 'Access-Control-Allow-Origin' header is present" cakephp

0

Good morning staff

I'm making an application with angular and cakePHP. I'm trying to make a request for the backend and the following message appears in the console:

  

XMLHttpRequest can not load link . In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access.

My controller

.controller('loginCtrl', ['$scope', '$stateParams', '$http', '$location', function ($scope, $stateParams, $http, $location) {

$scope.fazerLogin = function(usuario){
    //console.log(usuario);
    //http://localhost:8765/api/v1/users/client.json?client_id=cliente&client_secret=segredo"
    var url = "";
    var cid = "cliente";
    var cs = "segredo";
    $http.post("http://localhost:8765/api/v1/users/client.json?client_id="+cid+"&client_secret="+cs).success(function(data){
        console.log(data);
    });

}


}])

My php

<?php
public function beforeFilter()
{

   parent::beforeFilter();
           $this->response->header('Access-Control-Allow-Origin','*');
           $this->response->header('Access-Control-Allow-Methods','*');
           $this->response->header('Access-Control-Allow-Headers','X-Requested-With');
           $this->response->header('Access-Control-Allow-Headers','Content-Type, x-xsrf-token');
           $this->response->header('Access-Control-Max-Age','172800');
}
?>

Where do I have to make the adjustment in my php?

    
asked by anonymous 17.02.2017 / 14:37

2 answers

1

Hello! Home The server does not allow to make requests for another domain, besides the current one. You should give permission for this by adding the following header:

<?php
    header("Access-Control-Allow-Origin: *");
    ...
    ...
?>

Reference for more details: link

    
17.02.2017 / 18:18
0

At the endpoint of your service add:

$this->response->header('Access-Control-Allow-Origin', '*');
    
17.02.2017 / 14:43