Django Project - post a non-empty array using ajax but receive an empty one
My JS goes:
(data is not empty in both checkpoint1 and 2)
$("#btn_filesUpload").click(async ()=>{
let data = await getFiles() //checkpoint1
$.ajax({
url: '/files_upload/',
data: {'data': data},
type: 'post',
headers: {'X-CSRFToken': csrftoken },
}).done(function (response) { //checkpoint2
if (response.success){
console.log("Successfully uploaded")
}
else{
console.log("Uploading failed")
}
})
})
In view.py
def files_upload(request):
files = request.POST.getlist('data')
print(files)
print('----')
print(request.POST.getlist)
return JsonResponse({
'success': True
})
in the terminal:
[]
----
<bound method MultiValueDict.getlist of <QueryDict: {}>>
if i replace the data in ajax to
data:{'data':[1,2]}
it works just fine.
<bound method MultiValueDict.getlist of <QueryDict: {'data[]': ['1', '2']}>>
Does anyone know why this happens?