gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:59:35 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com