1

私は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>

そして、オブジェクトからフィールドを取得できません...助けてください

4

1 に答える 1

0

それは私の間違いでした。私のJSONはいくつかのレベルの辞書のようで、JSONオブジェクトの最初のレベルの辞書のフィールドという名前の辞書にあったため、フィールドが見つかりませんでした。今は大丈夫です:

var key="title"
var item=data["fields"]
alert(item[key])
于 2012-06-16T16:50:53.067 に答える