سؤال

I use Dajaxice library with django. These are my ajax.py methods:

def get_by_date_queryset():
#return model.objects.latest('created_on')
return Article.objects.all().order_by('created_on')

def example1(request):
""" Handling sorting requests """
jsonList = []
queryset = get_by_date_queryset()
json_serializer = serializers.get_serializer("json")()
return json_serializer.serialize(queryset, ensure_ascii=False)

There is HTML:

<script>
function my_js_callback(data){
    alert(data) //this gives me [object Object],[object Object],[object Object]
    alert(data[0]) //this gives [object Object]
    alert(data[0].title) // fail

}
</script>
<button type="button" onclick="Dajaxice.content.example1(my_js_callback);">Click Me!</button>

And i cant get my fields from the object though... help please

هل كانت مفيدة؟

المحلول

It was my mistake. My JSON was like several levels dict and i couldn't find my field beacuse it was in dictionary named fields in the JSON object first level dictionary. Now it is okay:

var key="title"
var item=data["fields"]
alert(item[key])
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top