纯前端修改点赞问题。
Change-Id: I99522d6492beb310ec048ff1191349c4a4436e09
diff --git a/Merge/front/src/api/search_jwlll.js b/Merge/front/src/api/search_jwlll.js
index e18efa3..e75abb0 100644
--- a/Merge/front/src/api/search_jwlll.js
+++ b/Merge/front/src/api/search_jwlll.js
@@ -17,7 +17,25 @@
},
...options
})
- return await response.json()
+
+ // 检查响应状态码
+ if (!response.ok) {
+ let errorData
+ try {
+ errorData = await response.json()
+ } catch {
+ errorData = { error: `HTTP ${response.status}` }
+ }
+ throw new Error(errorData.error || `HTTP ${response.status}`)
+ }
+
+ // 处理空响应(如204 No Content)
+ const contentType = response.headers.get('content-type')
+ if (contentType && contentType.includes('application/json')) {
+ return await response.json()
+ } else {
+ return {} // 返回空对象而不是尝试解析JSON
+ }
} catch (error) {
console.error('API请求错误:', error)
throw error
@@ -62,8 +80,7 @@
// 获取帖子详情
getPostDetail: async (postId) => {
return await request(`${WZY_BASE_URL}/posts/${postId}`)
- },
- // 点赞帖子
+ }, // 点赞帖子
likePost: async (postId, userId) => {
return await request(`${WZY_BASE_URL}/posts/${postId}/like`, {
method: 'POST',
@@ -78,11 +95,10 @@
body: JSON.stringify({ user_id: userId })
})
},
-
// 查看是否点赞
hasLiked: async (postId, userId) => {
const res = await request(
- `${WZY_BASE_URL}/posts/${postId}/like?user_id=${userId}`,
+ `${WZY_BASE_URL}/posts/${postId}/like/status?user_id=${userId}`,
{
method: 'GET'
}