9

I'm returning this in my view:

    data = {'val1' : 'this is x', 'val2' : True}
    return HttpResponse(data)

I want to use this information in the dictionary within my javascript. Kind of like this:

            function(data) {
                if (data["val2"]) {
                    //success
                    alert(data["val1"]);
                }
            }

However my javascript doesn't work. There is no alert popping up and I know that the dictionary has the information when it leaves my python view.

How can I read this information in my JS?


Ok so the answer for the view is to simplejson.dumps(data). Now when I do an alert(data) in my JS on my template I get {'val1' : 'this is x', 'val2' : True}. Now how can I manage the 2nd part of the question which is read out the values like

        function(data) {
            if (data["val2"]) {
                //success
                alert(data["val1"]);
            }
        }

UPDATE: The simplejson.dumps(data) converts th dictionary into string. So in the javascript you need to convert the string to an object. THis is the easiest but apparently unsafe way.

var myObject = eval('(' + myJSONtext + ')');
darren
  • 18,845
  • 17
  • 60
  • 79
  • 1
    possible duplicate of [Passing Python Data to JavaScript via Django](http://stackoverflow.com/questions/1445989/passing-python-data-to-javascript-via-django) – Felix Kling Jun 24 '11 at 12:32
  • 1
    Even if you use Ajax, the answers in the above question will help you. – Felix Kling Jun 24 '11 at 12:33
  • Are you using an AJAX (XmlHttpRequest) to make the request? – John Strickler Jun 24 '11 at 12:33
  • yes it's ajax and that question did help me understand that I must use jsdump in the view thanks. now i just want to know how to access the object in the template. – darren Jun 24 '11 at 13:13

4 Answers4

16

Very simply:

import json
data = {'val1' : 'this is x', 'val2' : True}
return HttpResponse( json.dumps( data ) )
underrun
  • 6,713
  • 2
  • 41
  • 53
  • ok i have this back in the JS {'val1' : 'this is x', 'val2' : True}. Now how can I access just the specific values. Like val1? like this data["val1"]? I get an undefined when I try alert(data["val1"]); – darren Jun 24 '11 at 13:12
  • Yes it still gives undefined :( – Sohaib Jul 02 '13 at 08:35
10

JSON is easiest way to transfer data(also you can use XML).

In python:

    import json
    data = {'val1': "this is x", 'val2': True}
    return HttpResponse(json.dumps(data))

In javascript:

    function (data) {
        data = JSON.parse(data);
        if (data["val2"]) {
            alert(data["val1"]);
        }
    }
levalex
  • 101
  • 2
  • 1
    This is the correct answer, or instead specifying mimetype like in the answer of Shwetabh Sharan below – mpaf Oct 29 '13 at 12:20
2

You can not directly use the python object you have to convert it into JSON string first Look into following documentation.

http://docs.python.org/library/json.html also http://www.json.org/

Sap
  • 5,197
  • 8
  • 59
  • 101
1

Just specify the mimetype in HttpResponse

    return HttpResponse(
                        json.dumps({"status":False, "message":"Please enter a report name."}) ,
                        content_type="application/json"
                        )
Deepa MG
  • 188
  • 1
  • 15
pass-by-ref
  • 1,288
  • 2
  • 12
  • 20