vendredi 28 septembre 2018

Ajax bind click to another button

I have a button (is anchor tag with class vote) which should be clicked to vote for an article via ajax request. After that I wanna update the votes count in my blade view (its a laravel application). The div with the class points should be updated but I dont know how to bind points on click to the ajax call? I hope you guys understand what I mean.. Maybe someone can help meout?

<a data-id="" class="btn vote "></a>

<div data-id="" class="points"></div>

$(document).on('click','.vote',function(){
 var $counter 
 var $button = $(this);
 var id = $button.data('id');
 var token = $('meta[name="csrf-token"]').attr('content');
$.ajax({

  type:'POST',
  url:"{!! URL::to('article/vote/') !!}/" + id,
  dataType: 'JSON',
  data: {
      "_method": 'POST',
      "_token": token,
      "id": id,
  },
  success:function(data){
      $('.points').html(data.count);
      var color = data.voted ? '#bbb' : '#38c172';
      $button.css('background-color', color);
      console.log('success');
      console.log(data);
  },
  error:function(xhr){
      if (xhr.status == 401) {
        window.location.href = "{!! URL::to('login') !!}";
      } else if (xhr.status == 403) {
        window.location.href = "{!! URL::to('email/verify') !!}";
      }
  },
});
});



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2xUGGv7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire