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

249
Visualizações
Looping over JSONResponse() result from django in jquery

I am interested in getting the results of an insert operation from views.py in form a JSON. I am getting the results alright, I think. my view looks like this:

added={}
if request.method=='POST':
        #Post method initiated.

        try:
            for id in allergies:
                   allergy=PatientAllergy(patient=patient,allergy_id=id,addedby=request.user)
               allergy.save()
               added[allergy.id]=id
        except BaseException as e:
            pass

return JsonResponse(added,safe=False)

The records, passed from JQUERY, added successfully to the database. What I want to get now is a JSON result in the form of {12:1, 13:2}.

My firebag shows the response as:

    12:1
    13:2

I am not sure if this a valid JSON or not. If I do list(added), it gives instead:

    0: 12
    1: 13

which I don't want. The problem I am having now I want to go thru the returned items but I am getting incorrect results. I basically want to get 12:1, 13:2.

         $.ajax({
            type: "POST",
            url: "/patient/addallergy/",
            data: postForm,
            dataType : "json",
            cache: "false",
            success: function (result) {

                    alert(result.length); //gives undefined, rendering the below line meaningless

                    if (result.length>0){


                        $.each(result,function(key,value){
                            alert(key);
                            alert(value);

                           });

                    }


            },
            fail: function (result){

            }


        }); 
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

change your views like this.

added_list=[]
    if request.method=='POST':
            #Post method initiated.

            try:
                for id in allergies:
                   added ={}                  allergy=PatientAllergy(patient=patient,allergy_id=id,addedby=request.user)
                   allergy.save()
                   added[allergy.id]=id
                   added_list.append(added)
            except BaseException as e:
                pass

    return JsonResponse(added_list,safe=False)

and in jquery

$.ajax({
        type: "POST",
        url: "/patient/addallergy/",
        data: postForm,
        dataType : "json",
        cache: "false",
        success: function (result) {

                alert(result.length); //gives undefined, rendering the below line meaningless

                if (result.length>0){

                     alert(JSON.stringify(result))
                    $.each(result,function(index,value){
                        console.log(value);



                       });

                     result.forEach( function (eachObj){
                       for (var key in eachObj) {
                                  alert(key);
                             alert(eachObj[key])
                            }
                       });

                }


        },
        fail: function (result){

        }


    });
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