Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('get_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..

like image 200
Grinart Avatar asked Nov 26 '25 21:11

Grinart


1 Answers

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
});
like image 68
vitorbal Avatar answered Nov 28 '25 12:11

vitorbal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!