I am using CakePHP 2.3.7.
I have a method called via ajax in my controller that is not successfully retrieving the data out of the session. I am doing the following.
I have an address form that is injected into the DOM from an ajax call. The user submits the form with invalid data. I get the errors and values out of the Address model and request data and write to the Session as follows:
public function index() {
//On form validation fail
$errors = $this->Address->validationErrors;
$data = $this->request->data;
$this->Session->write('Address', array('errors' => $errors,
'data' => $data));
}
Do a sanity check to make sure it's in the session:
var_dump($this->Session->read('Address'));
The page now reloads and JS fires this ajax method:
$.ajax({
url: '/orders/getAddress.html',
data: {"country" : country, "shipping" : shippingOption},
dataType: 'html',
cache: false,
success: function(data){
//data is the address form html
}
});
getAddress in Controller:
public function getAddress()
{
if($this->request->is('ajax')){
//Gather up data to create html form and put into an array
$data = array('states' => $states, etc...);
//Check for errors and values in session
if($this->Session->check('Address') == true){
$data['errors'] = $this->Session->read('Address.errors');
$data['values'] = $this->Session->read('Address.data');
}
$this->set(compact('data'));
$this->layout = 'ajax';
}
}
In most cases when getAddress() fires via ajax, the form renders, but the Session is empty and my errors and values are no longer available. It has gotten a bit maddening because it is not consistently repeatable. Some times my data makes it to the view, sometimes it does not. I've played around with my Session settings a bit, here is what they look like:
Configure::write('Session', array(
'defaults' => 'database',
'checkAgent' => false,
'ini', array('session.cookie_secure' => false, 'session.referer_check' => false)
));
I added the param
'session.cookie_httponly' => false
And had a little success in my development environment, but on production, the data is still lost in the Session on the ajax call.
I'm at the end of my rope! Suggestions appreciated.