S
S
s_katala2018-05-23 00:21:08
JavaScript
s_katala, 2018-05-23 00:21:08

How to fix 403 (Forbidden) Ajax?

127.0.0.1:8000/post-1/like 403 (Forbidden)

$(function(){
      $('body').on('click', '.article-like', function(){
      if ($(this).hasClass('fancybox-login-popup')) {
      return false;
      }

      var entryId = parseInt($(this).attr('data-id'));
      var hash = $(this).attr('data-hash');
      var sign = parseInt($(this).attr('data-sign'));

      var rating = $(this).parent().children('b');

      $.post('{% url 'posts:add_like' pk=post.pk %}', { entryId: entryId, sign: sign, hash: hash }, function(data) {
      if (data.error === undefined) {
      if (data.likesCount > 0) {
      var t = '+' + data.likesCount;
      var c = "positive";
      } else if (data.likesCount < 0) {
      var t = '–' + Math.abs(data.likesCount);
      var c = "negative";
      } else {
      var t = '0';
      }

      if (sign === 1) {
      var v = "voted-positive";
      } else {
      var v = "voted-negative";
      }

      rating.text(t);
      rating.parent().removeClass("negative positive").addClass(c);
      rating.parent().removeClass("voted-negative voted-positive").addClass(v);
      } else {
      showTip(data.error, 'error');
      }
      }, 'json');

      return false;
      });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mmm_corp, 2018-05-23
@mmm_corp

the error code indicates that you need to go through authorization,
they usually pass a "session cookie" in the request, but how everything is arranged there, only you know

M
marataziat, 2018-05-27
@marataziat

Disable CSRF protection

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question