0

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

8
  • 4
    do my eyes spot a typo inside your console.log? Commented Nov 23, 2016 at 12:13
  • @KevinKloet sorry, but I don't understand what is issue then Commented Nov 23, 2016 at 12:18
  • 1
    notifiications should be notifications Commented Nov 23, 2016 at 12:19
  • 1
    @Vinand compare data.notifications to data.notifiications Commented Nov 23, 2016 at 12:19
  • 1
    Set 'dataType' param to 'json' in your request params Commented Nov 23, 2016 at 12:38

2 Answers 2

1

You have a typo:

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

should be:

console.log(data.notifications[1].fields);
Sign up to request clarification or add additional context in comments.

3 Comments

You don't have "notification" in your object. This picture is bad, it would be more readable if it was a text.
If 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]'
change dataType: '' to dataType:'json'
0

Okay, not sure what was wrong, but this works just fine

$.ajax({
    url: '/notify/',
    type:'GET',
    dataType: 'json',
    success: function (data) {

        alert(data['notifications']);
         var sed = JSON.parse(data['notifications'])
        alert(sed[2].fields.choice_afl);

    }
  });

And in backend,as in EDIT2:

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

}
return Response(data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.