我在 python 中有一个使用 Django 的 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'}]}, {'word':'mcity_1','lists':[]},{'word':'mcity_2','lists':[]},{'word':'mydc','lists':[{' 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'}]}]}