在JS / jQuery中无法从序列化的python对象中获取数据 [英] Can't get data out of serialized python object in JS/jQuery

查看:267
本文介绍了在JS / jQuery中无法从序列化的python对象中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题的延续 AJAX调用Django视图(restframework endpoint)的内部错误,这是服务器端。现在有一个前端问题。

It's a continuation of the issue Internal error on AJAX call to a Django view (restframework endpoint) which was server side. Now there is a frontend problem.

$.ajax({
    url: '/notify/',
    type:'GET',
    dataType: '',
    success: function (data) {
      if (data.notifications) {
        console.log(data.notifiications[1].fields);

      }
    }
  });

在控制台中获取以下错误:

Get the following error in console:

TypeError: undefined is not an object (evaluating 'data.notifications')


$ b $在服务器端,一切都正确,我得到了我需要的任何数据。我想我需要首先解析它,但是当我试图解析它,它已经是一个对象。否则当我试图从对象中取出东西 TypeError:undefined不是一个对象

编辑:有一个打字错误,但问题依然存在。如果我打印到console.log:

There was a typo, but the problem remains. If I print it to console.log:

console.log(data.notifications);

没有什么。但是如果我提醒data.notifications:<对象对象],对象对象,对象对象,对象对象,对象对象,对象对象,对象对象,对象Object],[object Object]

there is nothing. But if I alert data.notifications: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

如果我像我这样提到像$ code> data.notifications [1 ] .model 或 data.notifications [1] .pk 或其他任何类似 data.notifications [1] .fields所有这一切在理论上必须是正确的,但什么都不返回。

And if I go further as I mentioned like data.notifications[1].model or data.notifications[1].pk or anything else like data.notifications[1].fields.whom all of this must be correct theoretically but return nothing.

TypeError: undefined is not an object (evaluating 'data.notifications.fields.choice_fl')

EDIT2:
也尝试手动设置字段

also tried setting fields manually

nots = serializers.serialize('json', Notification.objects.all(), fields=('whom','choice_afl'))
data = {
    'notifications': nots

}
return Response(data)

如果警报 alert(data ['notifications']); 获取此信息:

[{"pk": 1, "fields": {"whom": 1, "choice_afl": "F"}, "model": "blog.notification"}, {"pk": 2, "fields": {"whom": 1, "choice_afl": "F"}, "model": "blog.notification"}, {"pk": 3, "fields": {"whom": 1, "choice_afl": "F"}, "model": "blog.notification"}, {"pk": 4, "fields": {"whom": 1, "choice_afl": "F"}, "model": "blog.notification"}, {"pk": 5, "fields": {"whom": 1, "choice_afl": "F"}, "model": "blog.notification"}, {"pk": 6, "fields": {"whom": 1, "choice_afl": "F"}, "model": "blog.notification"}, {

和以前一样,进一步的,它是未定义的

And as previously whatever I enter further, it's undefined

推荐答案

你有一个打字错误:

console.log(data.notifiications[1].fields);

应该是:

console.log(data.notifications[1].fields);

这篇关于在JS / jQuery中无法从序列化的python对象中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆