【发布时间】:2020-07-07 19:27:28
【问题描述】:
因此,我将建立一个社交网络作为个人爱好,但最近,当我尝试为帖子点赞时,控制台日志会返回上述错误。代码在过去几天运行良好,但突然出现此错误。
点赞帖子的 Restapi 代码:
if ($_GET['url'] == "likes")
{
$postId = $_GET['id'];
$token = $_COOKIE['SNID'];
$likerId = $db->query('SELECT user_id FROM tokens WHERE token=:token', array(':token'=>sha1($token)))[0]['user_id'];
if (!$db->query('SELECT user_id FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$postId, ':userid'=>$likerId))) {
$db->query('UPDATE posts SET likes=likes+1 WHERE id=:postid', array(':postid'=>$postId));
$db->query('INSERT INTO post_likes VALUES (\'\', :postid, :userid)', array(':postid'=>$postId, ':userid'=>$likerId));
//Notify::createNotify("", $postId);
} else {
$db->query('UPDATE posts SET likes=likes-1 WHERE id=:postid', array(':postid'=>$postId));
$db->query('DELETE FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$postId, ':userid'=>$likerId));
}
echo "{";
echo '"Likes":';
echo $db->query('SELECT likes FROM posts WHERE id=:postid', array(':postid'=>$postId));
echo "}";
}
脚本设计:
$.ajax({
type: "GET",
url: "restapi/posts",
processData: false,
contentType: "application/json",
data: '',
success: function(r) {
var posts = JSON.parse(r)
$.each(posts, function(index) {
$('.timelineposts').html(
$('.timelineposts').html() + ' <li class="list-group-item" id="'+posts[index].postId+'" style="border-color: #cbcbcb;"><blockquote class="blockquote"><p class="mb-0" style="color: rgb(0,0,0);">'+posts[index].PostBody+'</p><footer class="blockquote-footer">Posted by '+posts[index].PostedBy+' on '+posts[index].PostDate+'</footer></blockquote><button data-id="'+posts[index].postId+'" class="btn btn-primary" type="button" style="background-color: rgba(0,0,0,0);color: rgb(0,0,0);width: 142px;font-family: Alegreya, serif;" > <i class="icon-fire" data-bs-hover-animate="rubberBand" style="color: rgb(36,0,255);" ></i> '+posts[index].Likes+' likes</button><button class="btn btn-primary" type="button" style="background-color: rgba(0,0,0,0);color: rgb(0,0,0);width: 142px;font-family: Alegreya, serif;" onclick="showCommentsModal()" data-postid="'+posts[index].postid+'" > <i class="typcn typcn-pencil" data-bs-hover-animate="rubberBand" style="color: rgb(255,0,0);"></i> Comentários</button> </li> </ul> '
)
$('[data-postid]').click(function() {
var buttonid = $(this).attr('data-postid');
$.ajax({
type: "GET",
url: "restapi/comments?postid=" + $(this).attr('data-postid'),
processData: false,
contentType: "application/json",
data: '',
success: function(r) {
var res = JSON.parse(r)
showCommentsModal(res);
},
error: function(r) {
console.log(r)
}
});
});
$('[data-id]').click(function() {
var buttonid = $(this).attr('data-id');
$.ajax({
type: "POST",
url: "restapi/likes?id=" + $(this).attr('data-id'),
processData: false,
contentType: "application/json",
data: '',
success: function(r) {
var ress = JSON.parse(r)
$("[data-id='"+buttonid+"']").html(' <i class="icon-fire" data-bs-hover-animate="rubberBand" style="color: rgb(36,0,255);"></i> '+ress.likes+' likes</span>')
},
error: function(r) {
console.log(r)
}
});
})
})
},
error: function(r) {
console.log(r)
}
});
});
已经验证了所有查询,它们在 sql 中工作正常。
编辑::
【问题讨论】:
-
请不要尝试创建自己的 JSON 字符串。相反,使用您的数据创建一个数组,然后使用
json_encode确保正确创建 json -
要准确验证返回的内容,请检查您的网络选项卡以获取来自服务器的响应。
-
@aynber 它返回这个
http://localhost/site/restapi/likes?id=112这很好,因为它是正确的 postid 但 JSON.parse 不会让我改变喜欢的数量 -
这是它返回的 URL。单击它,然后单击响应选项卡以查看返回的内容。
-
@aynber 此请求没有可用的响应数据。 所以问题出在 api...
标签: javascript php rest