简体   繁体   中英

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){

            }


        }); 

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){

        }


    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM