後台
app.get('/delBookType',function (req,res,next) {
var bookTypeList = '';
var delData = {_id:req.query._id};
bookTypeDao.delBookType(delData,function () {});
bookTypeDao.findBookType({},{},{},function (docs) {
console.log(docs);
bookTypeList = docs;
});
res.send({success:'操作成功',bookTypeList:bookTypeList});
})
前端
$.ajax({
url:'/delBookType',
type:'get',
data:{_id:idValue},
dataType:'json',
success:function (data) {
alert(data.success);
},
error:function (jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
})
<table class="table table-striped table-hover" id="book_type_table">
<thead>
<tr>
<th></th>
<th>唯一標識</th>
<th>圖書類型名稱</th>
</tr>
</thead>
<tbody>
<% bookTypeList.forEach(function(list,index){ %>
<tr>
<td><%= index+1 %></td>
<td class="id_td"><%= list._id %></td>
<td class="book_type_td"><%= list.bookTypeName %></td>
</tr>
<% }); %>
</tbody>
</table>
之前是寫java的所以習慣是在刪除操作之後再重新查一遍列表,但node這樣好像行不通,導致最後
res.send的docs取不到值。還有一個問題是我寫固定的JSON串放到bookTypeList參數裡面HTML也沒有覆蓋之前的列表,大神求教
//注意node的異步特性,這麼寫試試:
app.get('/delBookType',function (req,res,next) {
var bookTypeList = '';
var delData = {_id:req.query._id};
bookTypeDao.delBookType(delData,function () {});
bookTypeDao.findBookType({},{},{},function (docs) {
console.log(docs);
bookTypeList = docs;
res.send({success:'操作成功',bookTypeList:bookTypeList});
});
})