title: how to return a dictionary in python django and view it in javascript?
body: <p>I'm returning this in my view:</p>
  
  <pre><code>    data = {'val1' : 'this is x', 'val2' : True}
      return HttpResponse(data)
  </code></pre>
  
  <p>I want to use this information in the dictionary within my javascript. Kind of like this:</p>
  
  <pre><code>            function(data) {
                  if (data["val2"]) {
                      //success
                      alert(data["val1"]);
                  }
              }
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>How can I read this information in my JS?</strong></p>
  
  <hr>
  
  <p>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 <strong>{'val1' : 'this is x', 'val2' : True}</strong>.
  Now how can I manage the 2nd part of the question which is read out the values like</p>
  
  <pre><code>        function(data) {
              if (data["val2"]) {
                  //success
                  alert(data["val1"]);
              }
          }
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  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.</p>
  
  <pre><code>var myObject = eval('(' + myJSONtext + ')');
  </code></pre>