I have Django returning an HttpResponse of JSON type which looks like:
{"lat": "41.1391666667", "alt": " 367401.6875", "lon": " 113.945472222"}
The return statement from Django views.py method looks like:
return HttpResponse( simplejson.dumps( {'lat' : lat , 'alt' : alt , 'lon' : lon} ) , content_type='application/json')
I am attempting to read the Json using JQuery with the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
// The JSON (simplejson) Django repsonse looks like:
// {"lat": "23.5017777778", "alt": " 371405.03125", "lon": " 92.0223333333"}
document.write('Getting JSON response...');
$.getJSON(< my django url>,
function(data) {
alert(data['alt']);
});
});
</script>
</head>
<body>
</body>
</html>
I'm very new to Javascript / JQuery and have tried solutions from many other posts here but cannot seem to get any response running my JQuery code above ( I don't see any alert button with my altitude displayed). I've verfified the JSON response from Django looks fine. I would really appreciate some help on what I'm doing wrong here. Thanks alot.