我正在使用vue作为前端。我想在按下按钮时从数据库中删除一个对象,我使用axios发布选定的对象,但出现以下错误:
wish.js:40 Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)
即使我的对象确实从数据库中删除。
这是我的代码:
postWishToBeDeleted({commit}, wishId) {
console.log(wishId);
axios.post('/api/post/delete', {
wishId: wishId
}).catch(error => {
console.error(error);
}).then( response => {
commit('removeWish', wishId);
}
)
}
在我的symfony控制器内部:
/**
* @Route("/api/post/delete", name="app_api_post_delete", methods={"POST"})
*/
public function deleteWish(Request $request, WishRepository $repository) {
$data = $request->getContent();
$data = json_decode($data, true);
$wish = $repository->find($data['wishId']);
$em = $this->getDoctrine()->getManager();
$em->remove($wish);
$em->flush();
return $this->json($wish);
}
我认为我的回应有误,我对Vue和axios还是陌生的,所以我不确定如何正确返回json对象
编辑:
我注意到只有在我有多个对象的情况下才会发生此错误?如果我只是一个并且删除了它,就没有错误
状态500是服务器错误,并且从该行为中得知,删除该项目后,必须引起该错误的任何原因。
查看deleteWish
方法,如果$wish
已被删除,则可能是问题在未定义时试图将其转换为JSON。尝试返回其他内容,例如:
return true;