??yy 2015-02-02 10:22 采纳率: 0%
浏览 11

Yii Ajax问题

I've previously created a question where I wanted to understand principles of work with ajax in Yii. So, I'm trying to implement an answer. I'v created controller and placed in into controllers directory:

<?php
class AjaxController extends CController
{
    public function actionDoThing()
    {
        // Get request object
        $request = Yii::app()->request;

        // Check if request is acceptable
        if ($request->isPost && $request->isAjaxRequest)
        {
            echo CJSON::encode(array('hello'=>'world'));
        }
        // else
        // {
        //     throw new CHttpException(403);
        // }
    }
}
?> 

Also I have this fragment with script in my view.

<script>
$(document).on('click','div.lessonDiv', function() {
    $.ajax({
        type: "POST",
        url: <?php /*Also I tried just DoThing instead of actionDoThing, this doesn't work*/echo $this->createUrl('AjaxController/actionDoThing'); ?>,
        success: function(data, textStatus, jqXHR) 
        {
            console.log(data);
        }
    });
});
</script>

Dunno what's wrong, but console says:

Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'actionDoThing'

Where am I wrong now?

  • 写回答

2条回答 默认 最新

  • weixin_33704234 2015-02-02 10:31
    关注

    Looks like you forgot quotes around URL:

    $.ajax({
        type: "POST",
        url: "<?php echo $this->createUrl('AjaxController/actionDoThing'); ?>",
        success: function(data, textStatus, jqXHR) {
            console.log(data);
        }
    });
    
    评论

报告相同问题?