# Cakephp in back end of ionic application

**URL:** <https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421>\
**Category:** Need Help\
**Created:** [April 17, 2017, 10:34am UTC](https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421 "2017-04-17T10:34:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soufiane](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@soufiane](https://discourse.cakephp.org/u/soufiane)\
**Post date:** [April 17, 2017, 10:34am UTC](https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421/1 "2017-04-17T10:34:48Z")

</div>

hi every one  
i use cakephp in back end and when i try to login in my application ionic Knowing when i use method POST i get : Response for preflight has invalid HTTP status code 500 in console of my browser.

but when i use method GET all things are working but i noticed when i check data requeste i found that array is void nothing inside.  
i don’t know why when i use post is not working where is problem  
thanks.

```
var config = {
                    headers : {'Access-Control-Allow-Origin' : '*',
				'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
				'Content-Type': 'application/json',
				'Accept': 'application/json',
				'Access-Control-Allow-Headers':'X-Requested-With',	
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                    }

		}

		$http.post(link, {"data.User.password": pw,"data.User.username": email},config).then(function(response){
		    //console.info(response);
		    deferred.resolve(response.data);
		}, function(errResponse){
		    deferred.reject(errResponse);
		});

		return promise;
            }			
	}
    })

```

```
 public function login() {
		
        if ($this->Session->read('Auth.User')) {
			
            $this->set(array(
                'message' => array(
                    'text' => __('You are logged in!'),
                    'type' => 'error',
					'status'=> 200,
					'success'=>1
                ),
                '_serialize' => array('message')
            ));
        }

        if ($this->request->is('post')) {
			//debug($this->request->data);
			//die;
            if ($this->Auth->login()) {
				//debug($this->Session->read('Auth.User'));
			//die;
                //return $this->redirect($this->Auth->redirect());
				
           //debug('if **********************************');
			//die;
			
                $this->set(array(
                    'user' => $this->Session->read('Auth.User'),
                    '_serialize' => array('user')
                ));
            } else {
			//debug('else *************************');
				//die;
				$array_data = array_keys($this->request->data);
                $this->set(array(
                    'message' => array(
                        'text' => __('Invalid username or password, try again'),
						'type' => 'error',
						'status'=> 200,
					    'success'=>0,
						'data_0'=>$this->request->data
						//'data_1'=>$_GET['password'] 
                    ),
                    '_serialize' => array('message')
                ));
               // $this->response->statusCode(401);
            }
        }
    }

```

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/872fce36df9a602d857f5b0d4be0646c55dde31a.jpg)

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [April 17, 2017, 7:34pm UTC](https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421/2 "2017-04-17T19:34:49Z")

</div>

[https://book.cakephp.org/3.0/en/controllers/request-response.html#xml-or-json-data](https://book.cakephp.org/3.0/en/controllers/request-response.html#xml-or-json-data)

---

<div class="post-metadata">

**Author:** ![soufiane](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@soufiane](https://discourse.cakephp.org/u/soufiane)\
**Post date:** [April 18, 2017, 9:09am UTC](https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421/3 "2017-04-18T09:09:54Z")

</div>

i don’t have any problem with json data because when try to use postman it working i think for security reasons the browser

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [April 18, 2017, 11:53am UTC](https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421/4 "2017-04-18T11:53:36Z")

</div>

> [@soufiane](#):
>
> when i check data requeste i found that array is void nothing inside

> [@soufiane](#):
>
> when try to use postman it working

so its working or not working?

---

<div class="post-metadata">

**Author:** ![wanderarce](https://avatars.discourse-cdn.com/v4/letter/w/a6a055/32.png) [@wanderarce](https://discourse.cakephp.org/u/wanderarce)\
**Post date:** [September 25, 2017, 12:02pm UTC](https://discourse.cakephp.org/t/cakephp-in-back-end-of-ionic-application/2421/5 "2017-09-25T12:02:51Z")

</div>

Hi, this is solved for me.

public function beforeRender(Event $event)  
{  
// Note: These defaults are just to get started quickly with development  
// and should not be used in production. You should instead set “\_serialize”  
// in each action as required.  
$this-\>set(‘Auth’, $this-\>Auth);

```
    if (!array_key_exists('_serialize', $this->viewVars) &&
        in_array($this->response->type(), ['application/json', 'application/xml'])
    ) {
         $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');
        $this->set('_serialize', true);
    }
}
```
