Need Help with handling Json Object with PHP

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
tkwmm
Forum Newbie
Posts: 2
Joined: Wed Nov 18, 2015 2:33 am

Need Help with handling Json Object with PHP

Post 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};
tkwmm
Forum Newbie
Posts: 2
Joined: Wed Nov 18, 2015 2:33 am

Re: Need Help with handling Json Object with PHP

Post by tkwmm »

I solve it by change the

Code: Select all

data: formData
into

Code: Select all

data: JSON.stringify(formData)
:)
Post Reply