Node js and jQuery / Ajax (Like / Unlike)

I am working on my practical / game project (just started with jQ and Ajax), and the problem is that I cannot get my game as different from the button (mark / unmark in my application).

This is my code (some of node js stuff is also a button, I think it’s not a problem, because it works as expected (I can save the “tagged materials” in my database, etc.)):

  $('a.mark_button').on('click', function(event) { //marking a topic
        event.preventDefault();
        $(this).html('Unmark').removeClass('mark_button').addClass('unmark_button');

        $.ajax({
              url: '/mark',
              type: 'POST',
              contentType: 'application/json',
              dataType: 'json',
              data: JSON.stringify({ id:$(this).attr('id') }),
              complete: function() {
                    console.log('Process completed!');
              },
              success: function() {
                    console.log('Successfully');
              },
              error: function() {
                    console.log('Failed');
              }
        });
  });

  $('a.unmark_button').on('click', function(event) { //unmarking a topic
        event.preventDefault();
        $(this).html('Mark').removeClass('unmark_button').addClass('mark_button');

        $.ajax({
              url: '/unmark',
              type: 'POST',
              contentType: 'application/json',
              dataType: 'json',
              data: JSON.stringify({ id: $(this).attr('id') }),
              complete: function() {
                    console.log('Process completed');
              },
              success: function() {
                    console.log('Succesfully');
              },
              error: function() {
                    console.log('Failed');
              }
        });
  });

, , , "MARK", , ajax- ( .., ( )) , "UNMARK" ( facebook / ), , . , , , "unmark_message", "" . , , ajax .

, ( "" ).

+4
1

. , .

, , , ( ), ( ).

.

$('a.mark_button, a.unmark_button').on('click', function(e) {
  var $target = $(e.target);
  if ($target.hasClass('mark_button')) {
    onMarkedButtonClick($target);
  } else if ($target.hasClass('unmark_button')) {
    onUnmarkedButtonClick($target);
  }
});

onMarkedButtonClick onUnmarkedButtonClick , .

JSFiddle, /.

https://jsfiddle.net/reid_horton/hwkfjx6a/

+2

Source: https://habr.com/ru/post/1652370/


All Articles