如何使用json序列化将具有其他变量的对象发送回客户端?

时间:2019-07-18 14:24:41

标签: jquery json django ajax serialization

如何将字典a添加到response,如何在two objects获得ajax

视图

def abc(request):
    cp = Cp.objects.get(id=1)
    cp = serializers.serialize('json', [cp,])
    cp = json.loads(cp)

    a = {'a': 'a', 'b': 'b'}

    return HttpResponse(data)

js

$.ajax({
    //
    success: function(data){
    }    
})

1 个答案:

答案 0 :(得分:2)

Thnx @WillemVanOnsem寻求帮助

观看次数

def abc(request):
    cp = serializers.serialize('json', [cp,])
    cp = json.loads(cp)
    cp = json.dumps(cp)
    other = {'rate': float(rate), 'eair': float(eair), 'overpayment': overpayment, 'total_payment': round(total_payment), 'monthly_payment':round(monthly_payment)}
    return JsonResponse({'cp': cp, 'other': other})

js

  $.ajax({
    //
    success: function(data) {
      var cp_json = JSON.parse(data.cp);
      var other = data.other;
      var credit_payment = cp_json[0].fields;
      $('.rate').html(credit_payment.rate + '%')
    }
  })