Ajax POST returning NULL even with dataType set as 'json'

I am trying to show a JUI Dialog for a Delete confirmation, and then use ajax post for performing the delete action. The delete action is being successfully performed, but the JSON data is not being returned. Instead the data in the success function of the ajax call always shows as null.

CSRF Validation is also turned. (Earlier, it took me sometime to realise that for post ajax calls with CSRF validation, the csrf token also needs to be sent as data. The csrf part is working now.)

This is a stripped down version of what i am doing:

The JUI dialog widget




$this->beginWidget('zii.widgets.jui.CJuiDialog', array(

        'id'=>'deleteConfirmation',

        'options'=>array(

            'title'=>'Delete Confirmation',

            'autoOpen'=>false,

            'modal'=>TRUE,

            'minWidth'=>500,

            'buttons'=>array(

                'Delete'=>'js:function()

                    {

                        jQuery(this).dialog("close");

                        jQuery.ajax

                        ({

                            url: "' . $this->createUrl("delete") . '",

                            data: {' . Yii::app()->request->csrfTokenName . ':"' . Yii::app()->request->csrfToken . '"},

                            type: "POST",

                            dataType: "json",

                            success: function(data, textStatus, jqXHR)

                            {

                                alert(data); //for demonstration. Shows NULL


                                //document.location.href =  "' . $this->createUrl('index') . '";

                            }

                        });

                    }',

                'Cancel'=>'js:function(){jQuery(this).dialog("close"); return false;}',

            ),

        ),

    ));


        echo 'Are you sure you want to Delete';


    $this->endWidget('zii.widgets.jui.CJuiDialog');


    // the link that may open the dialog

    echo CHtml::button('Delete', array(

        'name'=>'Delete',

        'onclick'=>'jQuery("#deleteConfirmation").dialog("open"); return false;',

    ));



The controller action, for the sake of demonstration:




    public function actionDelete()

    {

        $response = array();

        $response['status'] = 'deleted';

        return CJSON::encode($response);

    }



Is this anyway connected to the usage of CSRF, or that of dialog?

Or am i missing something very basic?

You need to echo the json string instead of return.

Thanks for the reply…

Cannot believe that i spent almost 2 hrs yesterday trying to figure out the reason…