javascript - Issue with sending data to PHP from jQuery .ajax call -
alright using yii , need pass data controller method called events , jquery ajax call looks 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 send data calendar/events , "catched" events method, db fetching , bring data jquery uppo success. problem error logged , message (failed) console when doing it. empty string controllers method, weird. testing out simple code in it, looks this
public function actionevents() { $data = json_decode(yii::app()->request->getpost('data'),true); // assoc array returned if second param true echo json_encode($data); yii::app()->end(); }
i guessing problem lies in data object sent method without data={ json data here }, { json data here } without "data=" part. think? there way can "prefix" data object send php file "data="somehow ? appreciate help
jquery api docs $.ajax's data param "is converted query string". result, json_decode
ing useless. if want send json data, you'll need json.stringify
objecttosend
first. if this, should set appropriate content-type header.
apparently yii won't decode json post body according pr 2059 can use either php's json_decode
or yii's version , post body yii::app()->request->getrawbody()
.
my guess don't want json_decode
data , use post variables directly:
yii::app()->request->getpost('categories');
Comments
Post a Comment