javascript - 如何在 Yii2 中使用 post 请求在 ajax 调用中发送多个参数

标签 javascript php jquery ajax yii2

我有一个 View ,其中有一个 detailview 和一个 gridview。在我的 GridView 中,所有列都有复选框详细 View 包含模型 ID。现在情况很简单,我想从 GridView 中选择任何列,然后单击 a link 按钮,我想发送 ajax 调用,其中包括所选列的值和模型id,给我的 Controller 。以下是我的看法

<?= GridView::widget([
         'dataProvider' => $dataProvider,
          /*'filterModel' => $searchModel,*/

          'columns' => [

         ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($d) {
             return ['value' => $d['meter_id']];
         }],

         'Meter_Serial_Number',

         'Issued_To',
         'Store',
       ],
]); ?>
 <a href="<?= URL::toRoute(['ogpheader/viewsetpdf', 'id'=>$model->id])?>"  name="redirect" class="btn btn-primary" id="myid">Set PDF</a>

现在 javascriptajax 调用

<?php
$url = Url::toRoute(['/ogpheader/viewsetpdf','id'=>$model->id]);
$script = <<< JS
$(document).ready(function () {      

$('#myid').on('click',function() {


 var strValue = "";        
    $('input[name="selection[]"]:checked').each(function() {

    if(strValue!="")
        {
        strValue = strValue + " , " + this.value;

        }
    else 
        strValue = this.value;     

});
   // alert(strValue);
$.ajax({
     url: '$url',
     type: 'POST',
     data: {
           data: strValue,// also tired with {strValue:strValue id:id} but it did not worked for me as well              
     },         
     success: function(data) {
        alert(data);
     },
   });
  }) 
});
JS;
$this->registerJs($script, static::POS_END);
?>

Action Controller

public function actionViewsetpdf($id)
{
    $model = $this->findModel($id);
    print_r($_POST);
    $data = "";
    if(Yii::$app->request->isAjax)
    {
        $data = json_decode($_POST['data']);
        print_r($data);
    }
    else{
        echo 'no data';
    }
    exit();

}

我总是得到的响应是Array ( ) no data。我也调查了Passing two parameters in yii2 ajax request using jquery to a controllerYii2 extra parameter ajax in controller但两者似乎对我都有帮助。

注意:

据我了解,id 是 get 而 strValue 是 post。所以我对他们两个都很困惑。可能是我错了。

更新 1

画质不是很好

enter image description here

enter image description here

Xhr 中的响应是

array(1) {
  ["data"]=>
   array(1) {
     ["data"]=>
     string(26) "99 , 100 , 101 , 102 , 103"
    }
 }

如有任何帮助,我们将不胜感激。

最佳答案

阻止默认点击事件

$('#myid').on('click',function(e) {
  e.preventDefault();

关于javascript - 如何在 Yii2 中使用 post 请求在 ajax 调用中发送多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47263893/

相关文章:

php - 使用PDO连接方式

jquery - 为什么第二个独立的ajax响应要等到第一个ajax响应才能获取?

javascript - 我想在 cookie 中存储切换状态,还想在单击下一个列表时关闭上一个列表

javascript - Angular 显示来自 json 的单个记录

javascript - jQuery:点击更改 div 文本,只工作一次

PHP 中的 Javascript 条件格式

php - CodeIgniter 中表 A 中的位置但不是 B 中的位置

javascript - 恢复使用parentNode.removeChild删除的项目

javascript - 如何在java中实现反向ajax

jquery - 使用 css 卡住数据表插件 header