ajax - Yii2 使用 Ajax 提交链接
问题描述
我正在尝试在 Yii2 中为我的内容创建投票,所以我尝试使用“a”标签和小部件使用 Ajax 插入数据,但是使用 ajax 它不起作用,没有 ajax 它工作完美。但是当我使用 Ajax 时它不起作用,并且我在控制台中没有错误。我的视图代码是:
<script type="text/javascript">
$('document').ready(function(){
$('#btn-vote-up' + <?= $pst_id ?>).on("click", function(e){
$.ajax({
type: "POST",
data: {"value" : "Like"},
success: function(msg) {
console.log (<?= $pst_id ?>);
$('#note-up' + <?= $pst_id ?>).load(' #note-up' + <?= $pst_id ?>);
}
});
e.preventDefault();
});
});
</script>
<?= Html::a('<i class="ly-ic-favorite-plus"></i>', '#', [
'class' => 'btn-logout',
'id' => 'btn-vote-up'.$pst_id,
'data' => [
'params' => [
'value' => 'Like',
'pstIDL' => $pst_id,
],
],
]) ;
?>
<?php yii\widgets\Pjax::begin(['id' => 'note-up'.$pst_id]) ?>
<span><?= $total_up ?></span>
<?php yii\widgets\Pjax::end() ?>
我的小部件代码是:
if (Yii::$app->request->post('value') == 'Like')
{
$pstIDL = Yii::$app->request->post('pstIDL');
$modelLIKEPOST = $this->findLikePost($pstIDL);
AxVotePost::VoteUP($modelLIKEPOST, $this->usr_rid, $this->chn_id, $pstIDL);
header('Location: ' .Url::current() );
exit;
}
解决方案
您$pstIDL
在进行 ajax 调用时没有发送值,而只是value
在发送,您可以使用var data=$(this).data('params')
两者来获取pstIDL
,并且value
json
在调用中将其与数据一起发送,请参见下文
<script type="text/javascript">
$('document').ready(function(){
$('#btn-vote-up' + <?= $pst_id ?>).on("click", function(e){
var data= $(this).data('params');
$.ajax({
type: "POST",
data: data,
success: function(msg) {
console.log (<?= $pst_id ?>);
$('#note-up' + <?= $pst_id ?>).load(' #note-up' + <?= $pst_id ?>);
}
});
e.preventDefault();
});
});
</script>
推荐阅读
- c# - 如何更改 ListView 中的选定项属性
- java - Variable seems to change its type in one line without reason. AnyLogic SelectOutput not working correctly types not working correctly
- algorithm - 计数排序的下限为 O(n)
- c - 从 uint16_t 转换为 int32_t 需要中间转换为 int16_t
- java - Android Studio Java 位图工厂返回 null
- c# - 无法从实现它的子类中读取接口属性?
- java - 每次按下(单击)Jbutton 时如何在现有按钮组中创建一个新的 JButton 实例
- java - 当 @Around 用于 Spring AOP 时,不会检索数据
- python - 仅当代码在 Python 3.8 中使用 Selenium 在页面上找到某个单词时,如何才能启动代码序列?
- r - 当需要在主中进行对数转换时绘制辅助 Y 轴