使用ajax编辑时的未定义值

编程入门 行业动态 更新时间:2024-10-09 14:19:36

使用ajax<a href=https://www.elefans.com/category/jswz/34/1770921.html style=编辑时的未定义值"/>

使用ajax编辑时的未定义值

im试图使用ajax从我的数据库中获取数据。因此,在控制器中我得到了ID,但是在编辑模式时ID未定义。

这里是控制器中的代码:

router.post('/ajax/edit_groups/:id', async (req, res) => {
    console.log("edit")
    let [data_group, err] = await model.getById(req.params.id)
    console.log(req.params.id)

    console.log(data_group)
    res.json(data_group)
});

ejs代码:

<table id="groups_table" class="table table-striped table-bordered" style="width: 100%;font-size:14px;">
    <thead class="thead-dark">
        <tr style="text-align: center;">
            <th>Group Name</th>
            <th>Group Description</th>
            <th>Role</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <%  if(groupData){
            for(var i=0;i < groupData.length; i++){
            if(groupData[i].role == 1) groupData[i].role = "Admin";
            else groupData[i].role = "User";
        %>
        <tr>
            <td><%= groupData[i].name%></td>
            <td><%= groupData[i].desc%></td>
            <td><%= groupData[i].role%></td>
            <td> 
                <div class="text-center">
                <a href="#" class="data" title="Edit" data-id="<%= groupData[i].id%>">
                    <span class="fas fa-edit fa-lg"style="color: #000000; font-size: 15px;">
                </a>
                <a href="" title="Delete">
                    <span class="fas fa-trash-alt fa-lg" 
                     style="color: rgb(206, 17, 17); font-size: 15px;">
                </a>
                </div>
            </td>
        </tr>
    <% };%>
    <% }%>
    </tbody>
</table>

在这里,我得到了数据ID值。编辑模式代码:

ejs中的脚本:

$('.data').on('click', function(){
        axios.post('ajax/edit_groups/' + $(this).attr("data-id"))
        .then(function (response){
            console.log("in: ", $(this).attr("data-id"))
            $('#editGroups').modal('show');
            $('#id_group').val(response.data_group[0].id);
            $('#name').val(response.data_group[0].group_name);
            $('#desc').val(response.data_group[0].group_desc);
            $('#inputRole').val(response.data_group[0].role);

        }).catch(function (error){
            console.log(error)
    })
})

在这里,控制台日志的结果,data-id是未定义的。所以idk如何解决这个问题。

im试图使用ajax从我的数据库中获取数据。所以在控制器中我得到了ID,但是当去编辑模态时ID是不确定的。这是控制器中的代码:router.post('/ ajax / edit_groups /:id',async(...

回答如下:

由于ajax是异步的,因此无法直接调用响应。尝试在您的函数上添加异步等待,例如:

更多推荐

使用ajax编辑时的未定义值

本文发布于:2024-05-07 04:39:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   未定义   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!