Cakephp in back end of ionic application


#1

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);
            }
        }
    }


#2

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


#3

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


#4

so its working or not working?


#5

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);
    }
}