2

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..

0

2 Answers 2

8

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
});
Sign up to request clarification or add additional context in comments.

Comments

2

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.