Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery/Ajax类按钮第二次无法正常工作_Jquery_Python_Ajax_Django - Fatal编程技术网

jQuery/Ajax类按钮第二次无法正常工作

jQuery/Ajax类按钮第二次无法正常工作,jquery,python,ajax,django,Jquery,Python,Ajax,Django,我正试图为我的Django项目(Python的框架)创建一个类似Ajax的按钮。基本上,当Ajax调用成功时,我所做的是对index.html进行部分更新。第一次它工作正常,但是当第二次单击具有相同id的按钮时,它将重新加载整个页面,这不是我所期望的。有人能教我如何防止这种情况发生吗 我不确定Python方面是否与此有关,但我还是要粘贴一些代码。多谢各位 main.js $('button').on('click', function(event){ event.preventD

我正试图为我的Django项目(Python的框架)创建一个类似Ajax的按钮。基本上,当Ajax调用成功时,我所做的是对index.html进行部分更新。第一次它工作正常,但是当第二次单击具有相同id的按钮时,它将重新加载整个页面,这不是我所期望的。有人能教我如何防止这种情况发生吗

我不确定Python方面是否与此有关,但我还是要粘贴一些代码。多谢各位

main.js

  $('button').on('click', function(event){
      event.preventDefault();
      var element = $(this);
      $.ajax({
        url : element.attr("data-id") + '/like/',
        type : 'POST',
        data : { card_id : element.attr("data-id")},
        success : function(response){
          console.log(response);
          $('#like-area-' + element.attr("data-id")).load(document.URL +  ' #like-area-' + element.attr("data-id"));
        }
      })
    })


// using jQuery
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});
url.py

def like_post(request, card_id):
    if Like.objects.filter(liker=request.user, liked_post = card_id).exists():
        existing_like = Like.objects.get(liker=request.user, liked_post = card_id)
        existing_like.delete()
        likes = Like.objects.all()
        is_liked = 0
        # return HttpResponse(likes)
        return HttpResponse(is_liked) #not really using this yet
    else:
        card = Card.objects.get(id=card_id)
        new_like = Like(liker=request.user, liked_post = card)
        new_like.save()
        # likes = Like.objects.all()
        likes = Like.objects.all()
        is_liked = 1
        return HttpResponse(is_liked) #not really using this yet
url(r'^(?P<card_id>[0-9]+)/like/$', views.like_post, name='like-post'),
url(r'^(?P[0-9]+)/like/$,views.like_post,name='like-post'),

我刚读到你的请求:ajaxcầ不单击2次(需要重新加载或自动重新加载页面)

~>为ajax创建一个变量并中止它

var ajaxcall1
def ...
if (ajaxcall1)
        ajaxcall1.abort();
# some more code

你可以试试,我只是觉得你没有检查状态。如果没有,我将晚些时候查看更多详细信息。

$(文档)。在('click','button',function(event){//code here})
。。阅读,非常感谢!现在它就像一个符咒!
var ajaxcall1
def ...
if (ajaxcall1)
        ajaxcall1.abort();
# some more code