【问题标题】:Looping over JSONResponse() result from django in jquery在 jquery 中循环来自 django 的 JSONResponse() 结果
【发布时间】:2017-04-23 22:30:21
【问题描述】:

我有兴趣从views.py 以JSON 格式获取插入操作的结果。我想我得到的结果很好。我的观点是这样的:

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)

从 JQUERY 传递的记录已成功添加到数据库中。我现在想要得到的是 {12:1, 13:2} 形式的 JSON 结果。

我的 firebag 显示响应为:

    12:1
    13:2

我不确定这是否是有效的 JSON。如果我列出(添加),它会给出:

    0: 12
    1: 13

这是我不想要的。我现在遇到的问题是我想通过退回的物品,但我得到的结果不正确。我基本上想得到 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){

            }


        }); 

【问题讨论】:

  • "不确定它是否是有效的 json" ...网上有很多 json 验证器
  • 感谢 Charlietlf...

标签: jquery json django


【解决方案1】:

像这样改变你的观点。

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)

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

        }


    });

【讨论】:

  • 谢谢@Thameem。完美运行。
猜你喜欢
  • 1970-01-01
  • 2011-07-06
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2018-05-18
  • 2016-01-21
  • 2019-09-05
相关资源
最近更新 更多