laravel - Laravel vue axios post没有数据作为json响应返回
问题描述
我创建了一个简单的系统来评论帖子或视频。一切正常。
一个名为loadComments()的函数显示与帖子或视频相关的评论。
当用户提交新评论时,会创建评论,但为了查看新评论,我必须刷新页面,因为我收到未定义的响应。
这里是控制器中的存储功能
public function store(Request $request)
{
$comments = Comment::create([
'comment' => $request->comment,
'user_id' => auth()->user()->id,
'post_id' => $request->post_id
]);
return response()->json([
'comments' => $comments,
'success' => 'Comment created successfully!'
],200);
}
这里的 addComment 方法:
addNewComment() {
const formData = {
comment: this.comment,
post_id: this.propsVideoid
}
axios
.post(this.commentsUri, formData)
.then(response => {
this.comments.unshift(response.data.comments)
})
.catch(error => {
this.errors = [];
if(error.response.status == 422) {
this.errors = error.response.data.errors;
}
})
},
这里加载和添加方法一起
addNewComment() {
const formData = {
comment: this.comment,
post_id: this.propsVideoid
}
axios
.post(this.commentsUri, formData)
.then(response => {
this.comments.unshift(response.data.comments)
})
.catch(error => {
this.errors = [];
if(error.response.status == 422) {
this.errors = error.response.data.errors;
}
})
},
loadComments() {
axios.get(this.commentsListUri, {
params: {
post_id: this.propsVideoid
}
}).then(response => {
this.comments = response.data.comments;
});
},
},
mounted() {
this.loadComments();
}
addNewComment ()方法在数据库中插入新评论,但它不返回任何 json 响应。
解决方案
推荐阅读
- android - 底部导航片段状态
- javascript - 函数从 API 端点返回数据后回调函数未接收数据
- java - 我无法在不包含任何客户端类的 wsdl 链接类中实现方法
- java - 在有向图中查找模式
- c - 如何将 fread() 2 个结构放在一起 C
- c# - 无法访问代码隐藏文件中的条目?
- c# - 为什么我不能在字符串前使用@$ 前缀?
- java - 在超阵列内生成填充多边形
- programming-languages - booknooklearning.com 上的这是什么语言?
- laravel-5.6 - `406 Not Acceptable` 响应:错误 406 - 不可接受