我使用django的Dajaxice库。 这些是我的ajax.py方法:
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)
有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>
我不能从对象中获取我的字段......请帮助
答案 0 :(得分:0)
这是我的错。我的JSON就像几个级别的dict,我找不到我的字段,因为它是在JSON对象第一级字典中的字典命名字段中。现在没关系:
var key="title"
var item=data["fields"]
alert(item[key])