// Prevent the default link behavior
event.preventDefault();
const url = `/likeComment/${postId}/${commentId}`;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// The like count was successfully updated on the server
// Now, reload the page to reflect the new like count
location.reload();
}
};
xhr.send();
}`
2条答案
按热度按时间c86crjj01#
这是带有
href
的锚<a>
元素的默认行为。当你点击它可以导致页面跳转到顶部。更改为:
<button>
或<div>
应该可以解决这个问题。monwx1rj2#
我通过在元素中添加onclick来解决这个问题
然后使用AJAX来更新Like计数。