0

Trying to apply this ajax plugin for django https://github.com/yceruto/django-ajax I'v used ajaxPost successfully, but can't apply ajaxGet

In views.py. In python all data is printed fine.

    @ajax
    def notify(request):
     notifications = Notification.objects.filter(whom=request.user.profile)
     for acd in notifications:
        print(acd)
        print(acd.choice_afl)
        print(acd.whom)
        print(acd.who_did)
     return {'notifications': notifications}

In html:

    $('.notifications_button').click(function(){

    ajaxGet('/notify/', function(notifications){

         for(var acd in notifications){

             alert(acd.choice_afl)
            $('#the_very_nw').html(acd.choice_afl);

         }
})

If I alert noty I get "notifications", if I alert acd.choice_afl I get undefined (or just nothing if insert into html), despite in python I get the sting result I need . What is wrong? Doesn't python object transforms into JSON object?

EDIT: Also for safe I serialized django object into json like this. No changes

data = serializers.serialize("json",notifications)
return {'notifications': data}

EDIT2: I watched the structure of JSON

 var jsonPretty = JSON.stringify(JSON.parse(data),null,2); 
$('#the_very_nw').html(jsonPretty);

and it looks like this. So I missed 'fields' but it changed nothing. Still nothing is printed. And that is in case if I return HttpResponse :

[ { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 1 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 2 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 3 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 4 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 5 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 6 } ]
  • 1
    inspect the response body in browser dev tools network (or in browser by opening that url). I'm not a python dev but it sure does not look like you are encoding any json and the response can only contain one json object or array representation(with nested levels of course). What does response look like? Use jsonlint.com to validate it – charlietfl Nov 20 '16 at 17:00
  • @charlietfl check the edit please –  Nov 20 '16 at 22:19
  • 1
    I don't know this rails ajax wrapper but I'm sure it has error handling methods that you whould implement and check what the arguments return – charlietfl Nov 20 '16 at 22:27

1 Answers1

0

I think the error is in your JS code. Try rewriting the loop as follows:

for (var i = 0; i < notifications.length; i++) {
   var acd = notifications[i];
   $('#the_very_nw').html(acd.choice_afl);
}

Currently acd are just indeces of the array, not its elements (a good explanation here).

Community
  • 1
  • 1