AJAX帖子不发送参数

时间:2016-08-02 22:26:36

标签: jquery ajax django post

我无法弄清楚为什么AJAX不会在POST中发送任何参数。它返回403错误,可能是因为CSRFTOKEN因此我使用JOIN修饰了view,但现在它没有发送任何参数。

@csrfexempt

查看

$(document).ready(function(){

    var cancel_reservation = $('.cancel-reservation');
    var confirm_reservation = $('.confirm-reservation');
    var reservation_id = $(this).data('reservation_id');

    cancel_reservation.on('click',function(e){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url:'/ajax/reservation/cancel/',
            data:{reservation_id:reservation_id},
            success:function(){
                alert('canceled');
            }
        });

    })
});

@csrf_exempt def reservation_cancel(request): print request.POST id = request.POST['reservation_id'] va_models.Reservation.objects.get(id=id).delete() return JsonResponse({'status_code': 'success'}) 打印print request.POST

<QueryDict: {}>

提出:

request.POST['reservation_id'] 

你知道问题出在哪里吗?

2 个答案:

答案 0 :(得分:0)

尝试

data:{'reservation_id':reservation_id},

答案 1 :(得分:0)

尝试将contentType: "application/json", dataType: "json",添加到您的ajax标头