@RestController
@RequestMapping("/forum")
public class ForumController {
code
@Resource
private ForumService forumService;
// 发布帖子
@PostMapping("/post")
public String createPost(@RequestBody Post post) {
int result = forumService.createPost(post);
return result > 0 ? "Post created successfully" : "Post creation failed";
}
// 删除帖子(普通用户需要在业务层验证是否本人)
@DeleteMapping("/post/{id}")
public String deletePost(@PathVariable Integer id) {
int result = forumService.deletePost(id);
return result > 0 ? "Post deleted successfully" : "Post deletion failed";
}
// 修改帖子
@PutMapping("/post")
public String updatePost(@RequestBody Post post) {
int result = forumService.updatePost(post);
return result > 0 ? "Post updated successfully" : "Post update failed";
}
// 根据帖子ID获取帖子详情
@GetMapping("/post/{id}")
public Post getPostById(@PathVariable Integer id) {
return forumService.getPostById(id);
}
// 模糊查询帖子标题
@GetMapping("/posts")
public List<Post> searchPostsByTitle(@RequestParam String title) {
return forumService.searchPostsByTitle(title);
}
// 新增评论
@PostMapping("/comment")
public String createComment(@RequestBody Comment comment) {
int result = forumService.createComment(comment);
return result > 0 ? "Comment added successfully" : "Comment adding failed";
}
// 删除评论
@DeleteMapping("/comment/{id}")
public String deleteComment(@PathVariable Integer id) {
int result = forumService.deleteComment(id);
return result > 0 ? "Comment deleted successfully" : "Comment deletion failed";
}
// 修改评论
@PutMapping("/comment")
public String updateComment(@RequestBody Comment comment) {
int result = forumService.updateComment(comment);
return result > 0 ? "Comment updated successfully" : "Comment update failed";
}
// 获取某帖子的评论列表(包含回复)
@GetMapping("/comments/{postId}")
public List<Comment> getCommentsByPostId(@PathVariable Integer postId) {
return forumService.getCommentsByPostId(postId);
}
}
2index.js:65 路由守卫触发,username: user01
:5173/father/Forumindex:1 Access to XMLHttpRequest at 'http://localhost:8080/forum/posts?title=' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
[新] 使用 Edge 中的 Copilot 来解释控制台错误: 单击
GET http://localhost:8080/forum/posts?title= net::ERR_FAILED 401 (Unauthorized)
Assistant
根据你提供的信息和错误:
code
Access to XMLHttpRequest at 'http://localhost:8080/forum/posts?title=' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
...
GET http://localhost:8080/forum/posts?title= net::ERR_FAILED 401 (Unauthorized)