Reputation: 4261
I am facing a strange issue. I am using Django 1.6. I am getting json response from a view when calling from jquery ajax function. But, the 'data' does not have length property!
$.ajax({
url: request_url,
dataType: 'json',
success: function(data){
console.log(data);
console.log(data.length); //gives an error
The console.log prints
Object {2: "XX", 5: "YY"}
My View
def get_items(request, id):
item_list = Items.objects.filter(cat = id)
result = {}
items_dict = {}
for item in item_list:
items_dict[item .id] = item.name
return HttpResponse(json.dumps(items_dict), content_type="application/json")
Why this is happening?
Upvotes: 0
Views: 447
Reputation: 5006
Objects don't have .length property in JavaScript. Use the following to get the length:
Object.keys(<your-object-here>).length
Upvotes: 1