I have such stuff in my django view:
message = 'sometext'
rating = 7
data = {'message':message, 'rating':rating}
from django.utils import simplejson
return HttpResponse(simplejson.dumps(data))
and this in my site.js:
$.ajax({
'type': "POST",
'url': url,
'data': {'rating':rating},
'success': function(msg){
alert(msg['rating']);
$.modal('<p>'+msg+'</p>');
$(".simplemodal-container").css("height", "auto");
},
'error' : function(){
$.modal('<p>Hinnangu salvestamine ebaõnnestus</p>');
$(".simplemodal-container").css("height", "auto");
}
});
the simplemodel window returns me information like this:
{"rating": "7", "message": "sometext"}
alert on previous line just returns ‘undefined’
Why? Whats missing?
i read about javascript dictionaries here:
http://rick.measham.id.au/javascript/hash.htm
and according to that, the alert should display ‘7’ not ‘undefined’ or not?
Alan
Of course it contains that. That’s the JSON that has been returned to you. Use json2.js to decode it to JavaScript objects.