title: Accessing fields with Python CGI FieldStorage from a JQuery AJAX call
body: <p>I have written some JQuery that makes an ajax call to a Python script.  I've been successful in passing strings back and forth between them, but I'm getting weird behaviour from the CGI FieldStorage object that I'm using to get the input in the Python script.  I've also managed to send a simple object with key/value pairs, like this:</p>
  
  <pre><code>deptObj = { one : '1', two : '2'};
  
  $.ajax({
      data: deptObj,
      url: "simple.py",
      success: function(result) { console.log("Success!"); console.log(result); },
      error: function(request, error) { console.log("Error"); console.log(request); }
  });
  </code></pre>
  
  <p>Here is my python script, simple.py:</p>
  
  <pre><code>#!/usr/bin/env python
  import json
  import cgi
  import cgitb
  
  print "Content-type: application/json\n\n" 
  cgitb.enable() # display error messages in web browser
  
  fs = cgi.FieldStorage()
  
  print json.dumps(fs.keys())
  </code></pre>
  
  <p>When I run the javascript, it correctly prints out the keys in my object, i.e. <code>["two", "one"]</code> (I'm not sure why they're reversed).  I can also get the corresponding values by replacing the last line of simple.py with something like <code>print json.dumps(fs["one"].value)</code>.</p>
  
  <p>However, when I try to make a multi-level object (an object within an object), I get weird behaviour.  For example, I kept the deptObj that I had already created, then passed in <code>data: { departments: deptObj}</code> to the ajax call.  Then, when I tell simple.py to print out the keys, the result is <code>["departments[two]", "departments[one]"]</code> instead of what I would expect, <code>["departments"]</code>.  Then, since "departments" is apparently not a key, I get a KeyError when I try <code>print json.dumps(fs["departments"].value)</code>, while <code>print json.dumps(fs["departments[one]"].value)</code> gives me a result of 1.</p>
  
  <p>According to the <a href="http://docs.python.org/2/library/cgi.html" rel="nofollow">documentation for FieldStorage</a>, "the fields, accessed through form[key], are themselves instances of FieldStorage", so I thought that my FieldStorage object would have a "departments" key whose value is another FieldStorage object containing the keys "one" and "two".  However, this doesn't seem to be true.  How do I form a multi-level Javascript object and access it in my python script using FieldStorage?</p>