Jurgen Feuchter
Jurgen Feuchter

Reputation: 604

Send REST response on data using CakePhp

Hello Im having trouble with a service Im building. Right now I can see the body of the response in json and everything. But a friend who is getting the response on angular using $http.get('url'); seems to be having a hard time getting the data. Im not familiar with angular, so Im not sure how that call works and Im not sure why he cant get the data Im sending him. Here is the code:

                    $jsonResponse = array(
                        'success' => 'Llamada exitosa.',
                        'producto' => $responseproducts
                    );

                    $this->response->type('json');

                    $this->set('data', $jsonResponse);
                    $this->set('_serialize', 'data');

                    $json = json_encode($jsonResponse);
                    $this->response->body($json);

The $responseproducts is the array with the response I want him to get. The URL does return the array on the body when I try it on postman. And I can use the data on a ajax I build on a simple html to see if I could use the url. But for some reason my friend cant get the array on the data of the response. Any ideas what I might be doing wrong?

Upvotes: 1

Views: 462

Answers (1)

Jurgen Feuchter
Jurgen Feuchter

Reputation: 604

Hehe I found the answer. Digging a little bit more, it seems he had a problem with CORS availability. I just use the next code I found to let CORS on my service:

//Enabling CORS for CakePHP
    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');
    }

As I read on the page where I found this code, not all 5 are necessary. Most of the time it works only with the first one, but test for the rest if necessary. Hope this helps soemone :P

Upvotes: 1

Related Questions