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

247
Views
Recorriendo el resultado de JSONResponse() de django en jquery

Estoy interesado en obtener los resultados de una operación de inserción de views.py en forma de JSON. Estoy obteniendo los resultados bien, creo. mi vista se ve así:

 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)

Los registros, pasados desde JQUERY, se agregaron con éxito a la base de datos. Lo que quiero obtener ahora es un resultado JSON en forma de {12:1, 13:2}.

Mi firebag muestra la respuesta como:

 12:1 13:2

No estoy seguro de si este es un JSON válido o no. Si hago una lista (agregado), da en su lugar:

 0: 12 1: 13

que no quiero. El problema que tengo ahora es que quiero revisar los artículos devueltos, pero obtengo resultados incorrectos. Básicamente quiero obtener 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 answers
Answer question

0

cambia tus puntos de vista así.

 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)

y en 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 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!