Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
Pass Array to backend using Ajax (Django)

I am learning AJAX and I am having trouble passing an array to the backend. I can pass a simple variable and my code works great, but when I pass an array I am not able to pass the data successfully.

My code is below (this works):

    function add_var() {
    var aa = 5;

    $.ajax({
        url : "add_variable/",
        type : "POST", 

        data : { num1 : aa},
        
        success : function(json) {
            $('#num3').val(json); 
        },

        error : function() {
            console.log("fail");
        }
    });
    };

This works just fine and I can pass 5 to the backend. However when I change aa to an array, the function no longer works and returns a 'None' on the Views backend.

    function add_var() {
    var aa = [5,10,15];

    $.ajax({
        url : "add_variable/",
        type : "POST",

        data : { num1 : aa},
        
        success : function(json) {
            $('#num3').val(json); 
        },

        error : function() {
            console.log("fail");
        }
    });
    };

Could someone point me in the right direction here? Any help is appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do it that way for any string or numbers, but as soon as you need to pass back an object (such as array) you need to "stringify"

data : {num1 : JSON.stringify(aa)}

Then, you would have to "Parse" or "Deserialize" the string which converts it back to an object.

I believe in django, it would look something like this:

import json
nums = json.loads(num1)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!