Thursday, 18 November 2021

Refresh data without reloading the page

I have a function for adding likes on the page

blade.php

<a href="/article/?type=heart" class="comments-sub-header__item like-button">
<div class="comments-sub-header__item-icon-count">
  
</div>

<a href="/article/?type=finger" class="comments-sub-header__item like-button">
<div class="comments-sub-header__item-icon-count">
  
</div>

js

$(function() {
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
    },
  });

  $('.like-button').on('click', function(event) {
    event.preventDefault();

    let href = $(this).attr('href');

    $.ajax({
      url: href,
      type: 'POST',
      success: function() {
        window.location.reload();
      },
    });
  });
});

But when I click on the like to update the data, I reload the page using window.location.reload();

Can this somehow be done without reloading the page?



from Refresh data without reloading the page

No comments:

Post a Comment