POST jQuery array to Django

前端 未结 2 585
心在旅途
心在旅途 2021-02-01 20:11

I\'m trying to POST a jQuery array of simple numbers to Django, and I really can\'t make it work. I need a little help on this. I\'m getting an Http 500 with the following error

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 20:37

    You can try to use JSON.stringify() method instead of tasks as parameter when sending via Ajax.

    Here's the array data on my consol

    Then send Array data by ajax

    $.ajax({
         type: "POST",
         url: "/user/Survey-Design/",
         headers: {
             'Authorization': "Token " + localStorage.access_token
         },
         data: {
             'arr': JSON.stringify(arr)
         },
         success: function(result) {
             alert('Data Has been saved')
         }
     });
    

    In views.py:

     def create(self,request):
         array_data = request.POST['arr']
         data = json.loads(array_data)
         print(data)
         return HttpResponse('Success')
    

    Finally, show print(data) on terminal

提交回复
热议问题