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')
On server side everything is correct, and I get whatever data I need. I supposed that I need to parse it first but when I'm trying to parse, it's already an object. Otherwise when I am trying to get something out as out of the object TypeError: undefined is not an object.
EDIT: There was a typo, but the problem remains. If I print it to console.log:
console.log(data.notifications);
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]
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)
if alert alert(data['notifications']); get this:
[{"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
data.notificationstodata.notifiications