Yii 框架 Ajax 链接 [英] Yii Frameworrk Ajax Link

查看:38
本文介绍了Yii 框架 Ajax 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的索引帖子控制器按以下方式列出所有帖子

My index post controller list all posts in the following way

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'template'=>"{items}\n{pager}",
)); ?>

我的视图 _view 有 ajax 链接

My view _view has the ajax-link

<div id="comments"></div>
<?php echo CHtml::ajaxLink('List Comments', array('listComments'),
        array('update' => '#comments'))?>

listComments 是我的 PostController 中的一个函数

listComments is a function in my PostController

public function actionListComments()
    {
            $this->renderPartial('_comments',array(
            'post'=>$model,
            'comments'=>$model->comments,
            )); 

    }

当我点击ajax链接时,没有任何反应,

When I click to the ajax link , nothing happens,

它指向 localhost/blog/#

it points to localhost/blog/#

你能帮我吗?

推荐答案

很多事情可能会出错.由于 ajax 调用无法使用像

many things could go wrong about that. As ajax calls cant be debugged with normal compnonents like

CVarDumper::Dump();
die();

以上代码不会在浏览器区域显示任何内容.调试ajax 调用的最佳方法是使用inspectElement.点击网络.现在,当您单击 ajaxLink 时,它将显示 ajax 请求是否已成功发送.如果请求不成功,它将是红色的.当您单击提出的请求时.它会在右侧显示 3 个标签,名为 HeaderPreviewResponse.由于您要呈现页面,因此内容类型应为 text/html.

Above code will not show you anything in the browser area. The best way to debug ajax calls is using inspectElement. Click on Network. Now when you click on ajaxLink it will show you whether the ajax request was sent successfully. It will be red if the request was unsuccessful. When you click on the request made. It will show you 3 tabs on right named Header, Preview, Response. As you want to render the page so the content-Type should be text/html.

就您的代码而言,显然您正在使用 $model 而没有实例化它,因此它返回错误.阅读您的案例中返回的错误.

As far as your code is concerned clearly you are using $model without instantiating it so it is returning error. Read the error returned in your case.

这篇关于Yii 框架 Ajax 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆