jquery; django; parsing httpresponse
I have a problem with parsing http response.. I try send some values to client
>>>>return HttpResponse(first=True,second=True)
when parsing:
$.post('开发者_如何学Pythonget_values',"",function(data){
alert(data['first']); //The alert isn't shown!!!
});
what is the right way to extract values from httpresponse
maybe I make a mistake when create my response..
If you are trying to use json, you could do something like this:
Django
data = json.dumps({"FIRST":True, "SECOND":False})
return HttpResponse(data, mimetype="application/json")
and get it as:
jQuery
$.getJSON(url, [data], function(data){
alert(data['first']);
});
getJSON is a jquery shorthand function equivalent to the $.ajax function:
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
If you made your HttpResponse json:
return HttpResponse("{\"first\":true,
\"second\":false}")
then you could receive it as json
$.post('get_values',"",function(data){
alert(data['first']); //The alert isn't shown!!!
},"json");
精彩评论