I’ve a GAE app in python using Django. I’m trying to fetch a json from a python api using jquery’s get / getJSON api but it doesn’t read the passed string as json. Am I missing something here?
Django template
<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>
And here’s the 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)
And the json example
{‘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’}]}]}
should like following: