Javascript 使用ajax将序列化字符串发布到django
我正在尝试创建一个ajax函数,将列表发布到django视图中,但我做不好 我的javascript函数:Javascript 使用ajax将序列化字符串发布到django,javascript,jquery,python,ajax,django,Javascript,Jquery,Python,Ajax,Django,我正在尝试创建一个ajax函数,将列表发布到django视图中,但我做不好 我的javascript函数: function order_timers(data) { console.log(data) #Output: timer[]=38&timer[]=39&timer[]=25 $.ajax({ url: '{% url 'reorder_timers' %}', type: "POST",
function order_timers(data) {
console.log(data)
#Output: timer[]=38&timer[]=39&timer[]=25
$.ajax({
url: '{% url 'reorder_timers' %}',
type: "POST",
data: data,
success: function (response) {
console.log(response)
#Output: 383925
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError)
}
});
};
我的django观点:
@csrf_exempt
def reorder_timers(request):
timerlist = request.POST.getlist('timer[]') #think this is the line that does not work
i = 1
for item in timerlist:
timer = Timer.objects.get(pk=item)
timer.index = i
timer.save()
i = i + 1
return HttpResponse(timerlist)
有人知道我做错了什么吗?为什么要将数据类型设置为
json
?它不是JSON,它是标准格式编码的内容。删除了类型:JSON,它似乎在视图中发布了一些内容。但它似乎还是错了。怎么会错呢?发生了什么事?对不起。我的django代码有问题。它现在正在工作。谢谢你的帮助!