javascript - 未定义索引 : id in AJAX, 代码点火器

标签 javascript php ajax codeigniter

当我在 AJAX 中调用函数时,显示消息错误“未定义索引:id”,但如果在 View 中我有一个按钮,则可以工作,但当我有两个按钮时,会显示此错误。有什么问题?我需要为此创建一个数组或任何解决方案吗?

<tbody>
                <?php foreach ($fotos_concurso as $fotos) { ?>
                <tr>
                    <td><img width="200" height="200" src="<?php echo base_url('/uploads/'.$fotos['nombre_archivo']); ?>"></td>
                    <td><?php echo $fotos['nombre']; ?></td>
                    <td><a class="aprobar_foto" href="<?= base_url('admin/aprobar_foto') ?>" id="<?php echo $fotos['id'] ?>" title="aprobar"><i class="icon-trash"></i></a></td>
                    <td><a class="desaprobar_foto" href="<?= base_url('admin/desaprobar_foto') ?>" id="<?php echo $fotos['id'] ?>" title="desaprobar"><i class="icon-trash"></i></a></td>
                </tr>
                <?php } ?>
            </tbody>

以及 AJAX 代码

$('.aprobar_foto').on('click', function() {
            q = $(this);
            if (confirm('¿Quieres aprobar la foto?')) {
                $url = q.attr('href');
                $.ajax({ 
                    type: "post", 
                    url: $url, 
                    cache: false,   
                    data: "id=" + q.attr('id'), 
                    fail: function () { 
                        alert('Ha habido un error al borrar el registro');
                    },
                    success: function (redir) {
                        window.location = redir;
                    }
                });
            }
            return false;
        });

        $('.desaprobar_foto').on('click', function() {
            q = $(this);

            if (confirm('¿Quieres desaprobar la foto?')) {
                $url = q.attr('href');
                $.ajax({ 
                    type: "post", 
                    url: $url, 
                    cache: false,   
                    data: "id=" + q.attr('id'), 
                    fail: function () { 
                        alert('Ha habido un error al borrar el registro');
                    },
                    success: function (redir) {
                        window.location = redir;
                    }
                });
            }
            return false;
        });

最佳答案

像这样设置你的 id 字段:

var id = q.attr('id');
data: {id: id},

关于javascript - 未定义索引 : id in AJAX, 代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34850512/

相关文章:

javascript - 忽略 typescript 错误 "property does not exist on value of type"

javascript - Parse.com [Javascript API] : Get an array of parse objects

php - PHP登录验证之间有区别吗

PHP检查登录验证

php - 使用ajax更新股票价格

javascript - 重定向后获取实际图像 url

javascript - 如何跨框架访问元素?

php - jquery 和 ajax 页面刷新问题

javascript - FBJS Ajax加载内容

javascript - Angular $scope 将 push() 视为未定义