编辑时的未定义值"/>
使用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编辑时的未定义值
发布评论