无法在yii2中向控制器发送ajax响应

时间:2017-11-13 09:58:59

标签: php jquery ajax gridview yii2

我有一个视图,其中有detailviewgridview。在我的gridview我有一个复选框。点击任何复选框,我都可以获得所选列的id。以下是我的观点

//my DetailView
?= 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>

UI

Final UI that I get

注意:

detail view来自模型,gridviewquery生成,以查看然后选择数据。

现在我已经发出一个ajax调用,将数据发送到控制器。

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

$('#myid').click(function() {
 var strValue = "";

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

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

        }
    else 
        strValue = this.value;     

});
$.ajax({
     url: '$url',
     data: {
           data: strValue,              
     },
     type: 'post',
     dataType: 'json',
     success: function(data) {
        alert(data);
     },
  });
 }) 
});


JS;
$this->registerJs($script, static::POS_END);
?>

动作控制器

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

}

$id是模型的ID,即detail view的ID。我想通过我的detail viewgrid view个所选项目来处理事情。

现在当我点击Set PDF按钮时,我得到了以下结果

Output that i get

在输出图像中,我得到了我的模型的详细信息,即我的详细信息视图项,但对于我选择的网格视图项,我得到了no data。我已经搜索过但无法找到帮助我的解决方案

更新1

更改了我的JSajax来电

$('#myid').on('click',function(e) {
e.preventDefault();    
 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',
     dataType: 'json',
     data: 'data='+ strValue,         
     success: function(data) {
        alert(data);
      },
    });
   }) 
  });

我得到的Xhr中的输出是

common\models\Ogpheader Object( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 54 [created_by] => 12 [created_at] => 2017-11-13 05:26:04 [updated_at] => [canceled_at] => [store_id] => 10 [status] => Prepared [issuer] => 88 [admin_incharge] => Faisal [project_manager] => Ali ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 54 [created_by] => 12 [created_at] => 2017-11-13 05:26:04 [updated_at] => [canceled_at] => [store_id] => 10 [status] => Prepared [issuer] => 88 [admin_incharge] => Faisal [project_manager] => Ali ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ))array(1) { ["data"]=> string(26) "99 , 100 , 101 , 102 , 103"}

但是当我删除e.preventDefault()时,页面会被重定向到另一个视图,我得到的响应是

common\models\Ogpheader Object( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 54 [created_by] => 12 [created_at] => 2017-11-13 05:26:04 [updated_at] => [canceled_at] => [store_id] => 10 [status] => Prepared [issuer] => 88 [admin_incharge] => Faisal [project_manager] => Ali ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 54 [created_by] => 12 [created_at] => 2017-11-13 05:26:04 [updated_at] => [canceled_at] => [store_id] => 10 [status] => Prepared [issuer] => 88 [admin_incharge] => Faisal [project_manager] => Ali ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( )) no data

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

我认为$ url不适用于js代码, 例如,当你像这样点击#myid链接时,你可以通过jquery获取网址:

var url;
$('#myid').click(function() {
 var strValue = "";
   url = $(this).attr("href");
    $('input[name="selection[]"]:checked').each(function() {

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

        }
    else 
        strValue = this.value;

   $.ajax({
     url: url,
     data: {
           data: strValue,              
     },
     type: 'post',
     dataType: 'json',
     success: function(data) {
        alert(data);
     },
  });
 });

答案 1 :(得分:0)

你应该避免引用php var

$.ajax({
    url: $url,
    .....