Alright so I am using Yii and need to pass some data to the controller method called events and my jQuery ajax call looks like this
var objectToSend = { "categories" : [selectedOption],"datefrom" : month + "" + year , "dateto" : month + "" + year};
$.ajax({
url: '<?php echo Yii::app()->createUrl('calendar/events'); ?>',
type: "POST",
data: objectToSend,
success: function(data) {
console.log(data);
},
error: function() {
console.log('failed');
}
});
So what I would like to do is send the data to the calendar/events , which would then be "catched" by events method, do some DB fetching and bring back data to the jQuery uppo success. Problem is that I always get error logged and the message (failed) to console when doing it. I get an empty string back from the controllers method, which is weird. I am just testing it out with simple code in it, looks like this
public function actionEvents()
{
$data = json_decode(Yii::app()->request->getPost('data'),true); // assoc array is returned if second param is true
echo json_encode($data);
Yii::app()->end();
}
I am guessing the problem lies in data object sent to the method without data={ json data here }, but only as { json data here } without the "data=" part. What do you think? Is there a way I can "prefix" the data object send to PHP file with "data="somehow ? I appreciate all the help