从jQuery .ajax调用向PHP发送数据的问题

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

douhuan1979
douhuan1979 Ineersa非常感谢你的朋友!:)那个实际工作和POST数据最终有一个很好的设置,看起来像“data{”categories“:[”test-category-for-events“],”datefrom“:”82013“,”dateto“:”82013“}”
大约 7 年之前 回复
dongyong3590
dongyong3590 data:{data:objectToSend}试试这个。
大约 7 年之前 回复
dongwen9947
dongwen9947 发送到该方法的POST是“类别%5B%5D=test-category-for-events&datefrom=82013&dateto=82013”
大约 7 年之前 回复
dqmnueuu459279
dqmnueuu459279 出错时,我在记录数据时返回“null”。
大约 7 年之前 回复
dongmei8460
dongmei8460 我猜你可以使用类似于使用JQuery序列化表单时生成的数组,这样你就可以将“数据”名称设置为你要发送的信息。我建议使用Firebug来检查你究竟发送给服务器的是什么。
大约 7 年之前 回复

1个回答

jQuery API docs say that $.ajax's data param "is converted to a query string". As a result, json_decodeing it is useless. If you want to send JSON data, you'll probably need to JSON.stringify your objectToSend first. If you do this, you should also set an appropriate Content-Type header.

Apparently Yii won't decode a JSON POST body by itself but according to PR 2059 you can use either PHP's json_decode or Yii's version and get the POST body with Yii::app()->request->getRawBody().

My guess is that you probably don't want to json_decode your data and just use the POST variables directly:

Yii::app()->request->getPost('categories');
dongmeiyi2266
dongmeiyi2266 在对发送的数据进行字符串化后,我得到的POST看起来像“{”categories“:[”test-category-for-events“],”datefrom“:”82013“,”dateto“:”82013“}”
大约 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐