Yii2 renderAjax无法返回数据
我在过去的一年里做了这件事,效果很好。直到最近我才遇到这个问题,我不知道是什么原因造成的 在这里,我试图将供应商ID从(1)表单传递到(2)ajax javascript,并将(3)数据从AdminController返回到同一视图中的(4)divYii2 renderAjax无法返回数据,yii2,render,ajaxform,Yii2,Render,Ajaxform,我在过去的一年里做了这件事,效果很好。直到最近我才遇到这个问题,我不知道是什么原因造成的 在这里,我试图将供应商ID从(1)表单传递到(2)ajax javascript,并将(3)数据从AdminController返回到同一视图中的(4)div <div class="col-md-8 chatmessages"> </div> (1)表格:- <?php $form = ActiveForm::begin([ 'id'=>'chatform' ])
<div class="col-md-8 chatmessages">
</div>
(1)表格:-
<?php $form = ActiveForm::begin([
'id'=>'chatform'
]);?>
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</thead>
<tbody>
<?php
for($x = 0; $x < count($supplierChatInfoListTable); $x++){ ?>
<tr>
<td>
<?=$x+1?>
<div class="form-group">
<input type="hidden" name="supplierID" id="supplierID" class="form-control"
value="<?=$supplierChatInfoListTable[$x]['supplierID']?>" style="width: 50px">
</div>
</td>
<td >
<h4><?=$supplierChatInfoListTable[$x]['supplierFirstname']?></h4>
</td>
<td>
<button type="button" class="btn btn-primary btnchat"><i class="fa fa-edit"></i></button>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</tfoot>
</table>
</div>
<?php ActiveForm::end();?>
但它返回两个供应商的“相同”供应商ID,如下所示:
浏览器控制台给我这个错误?(更新):-
<?php $form = ActiveForm::begin([
'id'=>'chatform'
]);?>
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</thead>
<tbody>
<?php
for($x = 0; $x < count($supplierChatInfoListTable); $x++){ ?>
<tr>
<td>
<?=$x+1?>
<div class="form-group">
<input type="hidden" name="supplierID" id="supplierID" class="form-control"
value="<?=$supplierChatInfoListTable[$x]['supplierID']?>" style="width: 50px">
</div>
</td>
<td >
<h4><?=$supplierChatInfoListTable[$x]['supplierFirstname']?></h4>
</td>
<td>
<button type="button" class="btn btn-primary btnchat"><i class="fa fa-edit"></i></button>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</tfoot>
</table>
</div>
<?php ActiveForm::end();?>
顺便说一句,这是displaychatmessage.php的内容(我只是尝试显示supllierID及其名称)
直接聊天
3.
供应商ID:,
1月23日下午2:00
这个模板真的是免费的吗?真难以置信!
莎拉·布洛克
1月23日下午2:05
你最好相信!
-
发送
你能帮我吗?我不知道哪里出了问题。如您所述,单击两个供应商,您将获得相同的
供应商ID
。造成此问题的原因是,在js代码中,您通过输入的id获取supplierID
,即“#supplierID”
,并且根据html代码,两个供应商的输入元素在其id属性中具有相同的值,即“supplierID”,因为每次js都会从id属性值为“supplierID”的第一个输入元素获取值。因此,您需要对html和js代码进行一些更改。由于每个供应商都有单独的编辑按钮,因此不必使用输入来存储supplierID
值,您可以将其存储在编辑按钮的数据属性中。因此,更新后的html代码将成为
<?php $form = ActiveForm::begin([
'id'=>'chatform'
]);?>
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</thead>
<tbody>
<?php
for($x = 0; $x < count($supplierChatInfoListTable); $x++){
$supID = $supplierChatInfoListTable[$x]['supplierID']; ?>
<tr>
<td>
<?=$x+1?>
<div class="form-group">
<input type="hidden" name="<?='supplierID'.$supID?>" id="<?='supplierID'.$supID?>" class="form-control"
value="<?=$supID?>" style="width: 50px">
</div>
</td>
<td >
<h4><?=$supplierChatInfoListTable[$x]['supplierFirstname']?></h4>
</td>
<td>
<button type="button" class="btn btn-primary btnchat" data-supplierID="<?=$supID?>"><i class="fa fa-edit"></i></button>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</tfoot>
</table>
</div>
<?php ActiveForm::end();?>
<?php
$urproudcode= Url::to(['admin/chatinfo2pass']);
$this->registerJs("
$(function(){
$('.btnchat').click(function(){
var supplierID = $(this).attr('data-supplierID');
$.ajax({
'url':'".$urproudcode."',
'data':{supplierID:supplierID},
'method':'POST',
beforeSend: function (xhr) {
$.blockUI({
message: 'Processing...',
css: {
border: 'none',
padding: '15px',
backgroundColor: '#000',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
opacity: .5,
color: '#fff'
}
});
},
'success':function(data){
$('.chatmessages').html('Success');
if(data == 'false'){
$.unblockUI();
$('.chatmessages').html('Data False');
}else{
$.unblockUI();
$('.chatmessages').html(data);
}
},
error: function (xhr, ajaxOptions, thrownError) {
$.unblockUI();
$('.chatmessages').html('Failed');
console.log(data);
}
});
});
});
");
?>
我希望您的问题能够得到解决。从您的代码来看,您的Ajax请求似乎抛出了一些错误。您可以在浏览器控制台中检查错误,或使用代码查看错误消息。检查如何在浏览器控制台中查看Ajax错误,或者检查如何使用代码查看错误消息。在您的问题或此处的评论中添加Ajax错误消息和错误代码以获得进一步帮助。@MAZ,我已经按照您的建议添加了Ajax错误消息(在问题部分)。我应该从这里做什么?单击按钮后或之前(页面加载时)是否会出现此错误?您使用的是哪个jQuery版本?@MAZ,页面加载时更新的Ajax错误已经粘贴在上面的问题部分。你可以看看。它给了我参数1是数组,整数是给定的。这是什么意思?嘿@MAZ。我已经得到它来返回数据了!而不是使用
var dataform=$('#chatform')。serializeArray()代码>我使用了var supplierID=$('#supplierID').val()代码>。并将'data':dataform,
更改为'data':{supplierID:supplierID},
。但它返回相同的supplierID,即使我单击列表中的第二个供应商。请参阅上面的图片更新的Ajax错误消息,它给我找到了2个具有非唯一id#supplierID的元素。我想这与此有关,我明白了。它正在工作。你是说我基本上可以抛弃
元素,用数据供应商ID=
替换它,作为按钮中的元素之一。你是说无论我用以前使用的相同方法将多少
传递给ajax
,我仍然会得到与以前相同的错误?顺便说一句,谢谢你的帮助!是的,使用数据供应商ID=
是一种简单的方法。然而,您也可以使用
元素来实现这一点,但这会有点复杂。这意味着我仍然可以使用
元素实现这一点。我会记住这一点。谢谢你的帮助!没问题。很乐意帮忙!:)
<?php $form = ActiveForm::begin([
'id'=>'chatform'
]);?>
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</thead>
<tbody>
<?php
for($x = 0; $x < count($supplierChatInfoListTable); $x++){
$supID = $supplierChatInfoListTable[$x]['supplierID']; ?>
<tr>
<td>
<?=$x+1?>
<div class="form-group">
<input type="hidden" name="<?='supplierID'.$supID?>" id="<?='supplierID'.$supID?>" class="form-control"
value="<?=$supID?>" style="width: 50px">
</div>
</td>
<td >
<h4><?=$supplierChatInfoListTable[$x]['supplierFirstname']?></h4>
</td>
<td>
<button type="button" class="btn btn-primary btnchat" data-supplierID="<?=$supID?>"><i class="fa fa-edit"></i></button>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th>No</th>
<th>Supplier</th>
<th>Option(s)</th>
</tr>
</tfoot>
</table>
</div>
<?php ActiveForm::end();?>
<?php
$urproudcode= Url::to(['admin/chatinfo2pass']);
$this->registerJs("
$(function(){
$('.btnchat').click(function(){
var supplierID = $(this).attr('data-supplierID');
$.ajax({
'url':'".$urproudcode."',
'data':{supplierID:supplierID},
'method':'POST',
beforeSend: function (xhr) {
$.blockUI({
message: 'Processing...',
css: {
border: 'none',
padding: '15px',
backgroundColor: '#000',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
opacity: .5,
color: '#fff'
}
});
},
'success':function(data){
$('.chatmessages').html('Success');
if(data == 'false'){
$.unblockUI();
$('.chatmessages').html('Data False');
}else{
$.unblockUI();
$('.chatmessages').html(data);
}
},
error: function (xhr, ajaxOptions, thrownError) {
$.unblockUI();
$('.chatmessages').html('Failed');
console.log(data);
}
});
});
});
");
?>