Page 1 of 1

Need Help with handling Json Object with PHP

Posted: Wed Nov 18, 2015 2:39 am
by tkwmm
Hi, everyone, recently I try to handling json post request with php, below are my ajax code.

Code: Select all

var formData = {
                "size_id" : 2,
                "sizename" : some name
            };

$.ajax({
                type: 'post',
                url: '<?= Yii::app()->createAbsoluteUrl('mypage/doUpdate') ?>',
                dataType: 'json',
                data: formData,
                mimeType:"multipart/form-data",
                contentType: false,
                cache: false,
                processData:false,
                timeout: 120000,
                success: function(data) {
// some function here ...
            });
In my php , I got these code to handle the json, but I keep getting Trying to get property of non-object Error. Am I doing something wrong?

Code: Select all

$data = json_decode(file_get_contents('php://input'));
        $size_id = $data->{'size_id'};
        $sizename = $data->{sizename};

Re: Need Help with handling Json Object with PHP

Posted: Wed Nov 18, 2015 3:04 am
by tkwmm
I solve it by change the

Code: Select all

data: formData
into

Code: Select all

data: JSON.stringify(formData)
:)