Djangoを使用してPythonでGAEアプリを作成しました。jquery の get / getJSON API を使用して Python API から json をフェッチしようとしていますが、渡された文字列を json として読み取りません。ここで何か不足していますか?
Django テンプレート
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" > </script>
<script type="text/javascript">
$(document).ready(function(){
$.get('/test/api?request=getallwords', function(json) {
$('body').append(json);
var j = JSON.stringify(json)
$.each(json.details,function(i){
$('#list').append($('<li>' + this.word + '</li>'));
});
alert(j);
});
},'json');
</script>
</head>
<body>
<ol id="list">
</ol>
そして、これがpython APIです。
words = Word.all().fetch(20)
for w in words:
d = {"word":w.name.encode('utf-8'),"lists":[]}
for l in w.word_lists:
d["lists"].append({"list":l.word_list.name.encode('utf-8')})
success["details"].append(d)
self.response.headers['Content-Type'] = 'application/javascript'
self.response.out.write(success)
そしてjsonの例
{'response': 'success', 'details': [{'word': 'mcity', 'lists': [{'list': 'infy'}, {'list': 'dasd'}]}, {'単語': 'mcity_1', 'リスト': []}, {'単語': 'mcity_2', 'リスト': []}, {'単語': 'mydc', 'リスト': [{' list': 'infy'}]}, {'word': 'hyddc', 'lists': [{'list': 'infy'}]}, {'word': 'abysmal', 'lists': [ {'list': 'gre words'}]}, {'word': 'ascent', 'lists': [{'list': 'gre words'}, {'list': 'infy'}, {' list': 'mine'}]}, {'word': 'assda', 'lists': [{'list': 'dasd'}]}]}