[Answered ]-Undefined when trying to access json data (ajax)

2πŸ‘

βœ…

x in your loop is an index of the array, so you want: alert(data[x].model)

see for..in loop

Also, if you’re using Django 1.7+, please use JsonResponse

from django.http import JsonResponse

    ...

    else:
        objs = Tender.objects.all()[:4]
        return JsonResponse(objs)
πŸ‘€ben432rew

0πŸ‘

[ //<- Array 
    { //<- Object
        "model": "Register.tender",
        "pk": 1,
        "fields": { //<- Object
            "Name": "First",
            "Kind": "Public Trend",
            "Category": 1,
            "Description": "my first bid ",
            "Created_on": null,
            "Modified": null,
            "Active": true,
            "Size": "S",
            "Ministry": 1
        }
    }
]

Accessing

var Obj = data[0];
Obj.model;
Obj.pk;
Obj.fields.Name
Obj.fields...

OR

data[x].model;
data[x].pk;
data[x].fields.Name
data[x].fields...
πŸ‘€user2411670

Leave a comment