【发布时间】:2020-12-15 17:52:57
【问题描述】:
我用Vue 2.3和mongo,nodejs,express和handlebars,原来用handlebars,我可以删除
<form class="center"
action="/audioannotations/delete/{{_id}}?_method=DELETE" method="POST">
<input type="hidden" name="_method" value="DELETE">
<a
onclick="this.parentNode.submit()">
<i
class="fa fa-trash-o"
aria-hidden="true"
style="color: red;"></i>
Eliminar
</a>
</form>
但是用 vue 替换并尝试从数据库中 delete 数据,这就是我为此使用控制器和路由的方式: 并得到他的错误,会发生什么?
Error: Request aborted
createError webpack:///./node_modules/axios/lib/core/createError.js?:16
handleAbort webpack:///./node_modules/axios/lib/adapters/xhr.js?:73
这个值来自我的 id
5fd826c18f741e6eb1ea72c0
这个值 console.log(url)
/audioannotations/delete/5fd826c18f741e6eb1ea72c0
查看
<a href="" @click="borrarAudioanotacion(item2._id)">Delete </a>
控制器
borrarAudioanotacion(id) {
var currentUrl = window.location.pathname;
const url = `${currentUrl}/delete/${id}`;
// /audioannotations/delete/{{_id}}?_method=DELETE
this.axios.delete(url).then(
(response) => {
console.log("si se borro");
console.log(url);
},
(error) => {
console.log("no se borro " + "/audioannotations/delete/" + id);
console.log(url);
console.log(error);
}
);
},
路由器
router.delete(
'/delete/:audioannotation_id',
ensureAuthenticated,
ensureColabUser,
audioannotationsController.deleteAudioannotaion
)
【问题讨论】:
-
尝试
@click.prevent="borrarAudioanotacion(item2._id)"阻止页面导航和取消您的 AJAX 请求 -
添加
@click.prevent="borrarAudioanotacion(item2._id),我现在可以删除,但显示此消息 请求失败,状态码为 400
标签: javascript node.js express vue.js handlebars.js