【发布时间】:2012-05-04 04:32:49
【问题描述】:
我有一个使用 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'} ]}, {'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'}]}]}
【问题讨论】:
标签: jquery python django google-app-engine google-cloud-datastore