php 如何在yii2中进行ajax调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24199595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:12:44  来源:igfitidea点击:

How to make ajax call in yii2?

phpyii2

提问by mohi

In yii version 1.14 we used

在 yii 1.14 版中,我们使用了

CHtml::ajaxlink

CHtml::ajaxlink

for ajax call what about in yii2?

对于 ajax 调用在 yii2 中呢?

回答by Dency G B

You can make an ajax link like

您可以制作一个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;",
                ]);

回答by NinjaCat

From: http://www.yiiframework.com/wiki/665/overcoming-removal-of-client-helpers-e-g-ajaxlink-and-clientscript-in-yii-2-0/

来自:http: //www.yiiframework.com/wiki/665/overcoming-removal-of-client-helpers-eg-ajaxlink-and-clientscript-in-yii-2-0/

You can easily create and combine all such client helpers for your need into separate JS files. Use the new AssetBundle and AssetManager functionality with the View object in Yii2, to manage these assets and how they are loaded.

Alternatively, inline assets (JS/CSS) can be registered at runtime from within the View. For example you can clearly simulate the ajaxLink feature using a inline javascript. Its however recommended if you can merge where possible, client code (JS/CSS) into separate JS/CSS files and loaded through the AssetBundle. Note there is no more need of a CClientScript anymore:

您可以轻松地根据需要创建所有此类客户端助手并将其组合到单独的 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

回答by mohi

$.get( "' . Url::toRoute('controller/action') . '", { item: $("#idoffield").val()} ) /* to send the parameter to controller*/
                        .done(function( data )
                            {
                                 $( "#lists" ).html( data );
                                    }) 

and give lists id for div

并为 div 提供列表 ID

<div id="lists"></div>

for more visit https://youtu.be/it5oNLDNU44

更多信息请访问https://youtu.be/it5oNLDNU44

回答by Pushparaj Yuvaraj

<?=yii\helpers\Url::toRoute("site/signup")?>