Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda