如何从javascript文件向Yii2视图传递参数?

如何从javascript文件向Yii2视图传递参数?,javascript,php,yii2,Javascript,Php,Yii2,我有一个js文件,我正在通过它调用ajax来呈现一个视图文件并传递一些数据。如何做到这一点?这是我的ajax调用 $.ajax({ type: "GET", url: "index.php?r=orders/on-select", data: {myVar: myVar}, success: function (data) { //I want to render the view here and pass

我有一个js文件,我正在通过它调用ajax来呈现一个视图文件并传递一些数据。如何做到这一点?这是我的ajax调用

$.ajax({
        type: "GET",
        url: "index.php?r=orders/on-select",
        data: {myVar: myVar},
        success: function (data) {
            //I want to render the view here and pass the data
        },
    });

首先理解这个概念,无论您是在使用核心Php还是某些Php框架,ajax()及其工作原理都是一样的

$.ajax({
    url : 'your url',
    type: 'get'  // 'get' / 'post'
    data: {
        var1 : val1,
        var2 : val2,
        var3 : val3,
        // and so on
    }
});
您可以根据需要在key:value对中传递任意多个参数,并在php中获取其值,如:


$\u POST['var1']
$\u GET['var1']

首先了解ajax()的概念,无论您使用的是核心Php还是某些Php框架,它的工作原理都是一样的

$.ajax({
    url : 'your url',
    type: 'get'  // 'get' / 'post'
    data: {
        var1 : val1,
        var2 : val2,
        var3 : val3,
        // and so on
    }
});
您可以根据需要在key:value对中传递任意多个参数,并在php中获取其值,如:


$\u POST['var1']
$\u GET['var1']

是的,您可以使用ajax调用来完成它

$.ajax({
    type: "GET",
    url: "index.php?r=orders/on-select",
    data: {myVar: myVar},
    success: function (data) {
        //I want to render the view here and pass the data
    },
});
现在,在控制器中渲染所需的视图并回显它

    public function actionNoSelect(){
             if(isset($_REQUEST['myVar'])){
                $html = $this->renderPartial('path to your view file',[
               'model' => $model///// passing data to your view file if you want
],true)
echo $html;
}
    }
现在,在ajax成功函数中,您可以在一些div中显示它,如

success: function (data) {
      $('#divid').html(data);
    },

是的,您可以使用ajax调用来实现它

$.ajax({
    type: "GET",
    url: "index.php?r=orders/on-select",
    data: {myVar: myVar},
    success: function (data) {
        //I want to render the view here and pass the data
    },
});
现在,在控制器中渲染所需的视图并回显它

    public function actionNoSelect(){
             if(isset($_REQUEST['myVar'])){
                $html = $this->renderPartial('path to your view file',[
               'model' => $model///// passing data to your view file if you want
],true)
echo $html;
}
    }
现在,在ajax成功函数中,您可以在一些div中显示它,如

success: function (data) {
      $('#divid').html(data);
    },

请解释清楚。。ajax调用是正确的。。服务器端有问题吗。。请解释清楚。。ajax调用是正确的。。服务器端有问题吗。。