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

506
Visualizações
Pass Array using AJAX in Django

I have an array called metrics and im trying to pass it to backend with AJAX but Im only getting the last values of the array 7,8,9 . I want to pick up the whole array .Any idea how? Any help is appreciated.

html

 var metrics = [
  [1,2,3],
  [4,5,6],
  [7,8,9], 
];

 $.ajax({
        type : 'GET' , 
        url : "...",
        data : {"metrics[]": metrics },
    })

views.py

def Sensor_Metrics(request):
    if request.is_ajax and request.method == "GET" : 
        metrics = request.GET.get("metrics[]")
        print(metrics)
    return JsonResponse({"error": ""}, status=400)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The solution is adding the brackets in views.py while calling the request.

in script

 var metrics = [
  [1,2,3],
  [4,5,6],
  [7,8,9], 
];

 $.ajax({
        type : 'GET' , 
        url : "...",
        data : {"metrics": metrics},
    })

in views.py

def Sensor_Metrics(request):
    if request.is_ajax and request.method == "GET" : 
        metrics = request.GET.get("metrics[]") #Important
        print(metrics)
    return JsonResponse({"error": ""}, status=400)

You are welcome

over 4 years ago · Santiago Trujillo Relatório

0

In the views, you need to use request.GET.getlist in order to get the complete list from the request. You need to use -: metrics = request.GET.getlist("metrics[]"). You can refer the docs here.

over 4 years ago · Santiago Trujillo 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