【问题标题】:Passing two parameters in yii2 ajax request using jquery to a controller使用jquery将yii2 ajax请求中的两个参数传递给控制器
【发布时间】:2016-10-03 14:55:42
【问题描述】:

按下时我有一个链接,它通过渲染 ajax 从控制器请求页面,以前我只传递 id,但现在我想向控制器传递一个额外的参数,我如何实现这一点, 这是我尝试过的

这是仅将单个参数传递给控制器​​的链接

Html::a('click me', ['#'],
['value' => Url::to('checktruck?id='.$model->id), //this is where the param is passed 
'id' => 'perform']);

这是需要 2 个参数的控制器代码:

public function actionChecktruck($id,$category)  //it expects 2 parameters from above link
{
     $truckdetails = Truck::find()->where(['id' =>$id])->one();

    if (Yii::$app->request->post()) {
      $checklistperform = new TodoTruckChecklist();
        $truck = Truck::find()->where(['id'=>$id])->one();
        $checklistperform->truck_id=$id;
        $checklistperform->registered_by=Yii::$app->user->identity->id;
        $checklistperform->save();
        $truck->update();

        var_dump($checklistperform->getErrors());
        //var_dump($truck->getErrors());
    }
    else {
        $truckcategory = Checklist::find()->where(['truck_category'=>$truckdetails->truck_category])->andWhere(['checklist_category'=>$category])->all();
        return $this->renderAjax('truckyard/_checklistform', [
            'truckcategory' => $truckcategory,'truckvalue'=>$id,
        ]);

    }

}

这是我在发布请求期间依赖于上述控制器的另一个按钮的 jquery 代码

$("#postbutn").click(function(e) {

   $.post("checktruck?id="+truckid,  //here i would like to pass 2 params
                {checked:checked,unchecked:unchecked,truckid:truckid}
            )
 }

这是没有post时的jquery代码

如何在链接中传递额外的参数,甚至是控制器的 $.post 请求

【问题讨论】:

    标签: javascript jquery ajax yii


    【解决方案1】:

    首先,由于您使用的是JQuery ajax提交表单,所以不需要为链接设置值

    Html::a('click me', ['#'],['id' => 'perform']);
    

    使用这个id你可以提交如下请求

    $this->registerJs("$('#perform').click(function(event){
    
    event.preventDefault(); // to avoid default click event of anchor tag
    
    $.ajax({
        url: '".yii\helpers\Url::to(["your url here","id"=>$id,"param2"=>param2])."',        
        success: function (data) {
               // you response here
            },
          });
    });");
    

    方法属性不用说'POST',你要通过GET方法发送

    最后在你的控制器中,你需要接受如下参数

    public function actionChecktruck()  //it expects 2 parameters from above link
    {
         $id = Yii::$app->request->queryParams['id'];
         $param2 = Yii::$app->request->queryParams['param2'];
    
         $truckdetails = Truck::find()->where(['id' =>$id])->one();
    
        if (Yii::$app->request->post()) {
          $checklistperform = new TodoTruckChecklist();
            $truck = Truck::find()->where(['id'=>$id])->one();
            $checklistperform->truck_id=$id;
            $checklistperform->registered_by=Yii::$app->user->identity->id;
            $checklistperform->save();
            $truck->update();
    
            var_dump($checklistperform->getErrors());
            //var_dump($truck->getErrors());
        }
        else {
            $truckcategory = Checklist::find()->where(['truck_category'=>$truckdetails->truck_category])->andWhere(['checklist_category'=>$category])->all();
            return $this->renderAjax('truckyard/_checklistform', [
                'truckcategory' => $truckcategory,'truckvalue'=>$id,
            ]);
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      试试这个, 在查看文件中

      $this->registerJs("$('#postbutn').click(function(){
      $.ajax({
          url: '".yii\helpers\Url::to(["u r URL"])."',        
          method: 'POST',       
          data: {id:id, truckid:truckid },
          success: function (data) {
              },
            });
      });");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        • 2020-05-09
        相关资源
        最近更新 更多