[英]How to make ajax call in yii2?
在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;",
]);
來自: http : //www.yiiframework.com/wiki/665/overcoming-removal-of-client-helpers-eg-ajaxlink-and-clientscript-in-yii-2-0/
您可以輕松地創建所需的所有客戶端幫助程序並將其組合到單獨的JS文件中。 將新的AssetBundle和AssetManager功能與Yii2中的View對象一起使用,以管理這些資產及其加載方式。
或者,可以在視圖中在運行時注冊內聯資源(JS / CSS)。 例如,您可以使用內聯javascript清楚地模擬ajaxLink功能。 但是,如果可以將客戶端代碼(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>
<?=yii\helpers\Url::toRoute("site/signup")?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.