【发布时间】:2017-03-24 17:33:31
【问题描述】:
在 yii 1.14 版本中我们使用了
CHtml::ajaxlink
对于 ajax 调用在 yii2 中呢?
【问题讨论】:
-
以下答案是否需要 csrf 令牌?
-
Here 是一个例子
在 yii 1.14 版本中我们使用了
CHtml::ajaxlink
对于 ajax 调用在 yii2 中呢?
【问题讨论】:
你可以做一个像这样的ajax链接
Html::a('Your Link name','controller/action', [
'title' => Yii::t('yii', 'Close'),
'onclick'=>"$('#close').dialog('open');//for jui dialog in my page
$.ajax({
type :'POST',
cache : false,
url : 'controller/action',
success : function(response) {
$('#close').html(response);
}
});return false;",
]);
【讨论】:
您可以轻松地为您的 需要放入单独的 JS 文件中。使用新的 AssetBundle 和 AssetManager Yii2 中 View 对象的功能,用于管理这些资产和 它们是如何加载的。
或者,内联资产 (JS/CSS) 可以在运行时注册 从视图中。例如,您可以清楚地模拟 ajaxLink 功能使用内联 javascript。但是,如果 您可以在可能的情况下将客户端代码 (JS/CSS) 合并为单独的 JS/CSS 文件并通过 AssetBundle 加载。注意没有了 不再需要 CClientScript:
$script = <<< JS
$('#el').on('click', function(e) {
$.ajax({
url: '/path/to/action',
data: {id: '<id>', 'other': '<other>'},
success: function(data) {
// process data
}
});
});
JS;
$this->registerJs($script, $position);
// where $position can be View::POS_READY (the default),
// or View::POS_HEAD, View::POS_BEGIN, View::POS_END
【讨论】:
$.get( "' . Url::toRoute('controller/action') . '", { item: $("#idoffield").val()} ) /* to send the parameter to controller*/
.done(function( data )
{
$( "#lists" ).html( data );
})
并为 div 提供列表 ID
<div id="lists"></div>
更多信息请访问https://youtu.be/it5oNLDNU44
【讨论】:
<?=yii\helpers\Url::toRoute("site/signup")?>
【讨论】: