javascript - Accessing fields with Python CGI FieldStorage from a JQuery AJAX call -
i have written jquery makes ajax call python script. i've been successful in passing strings , forth between them, i'm getting weird behaviour cgi fieldstorage object i'm using input in python script. i've managed send simple object key/value pairs, this:
deptobj = { 1 : '1', 2 : '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); } }); here python script, simple.py:
#!/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()) when run javascript, correctly prints out keys in object, i.e. ["two", "one"] (i'm not sure why they're reversed). can corresponding values replacing last line of simple.py print json.dumps(fs["one"].value).
however, when try make multi-level object (an object within object), weird behaviour. example, kept deptobj had created, passed in data: { departments: deptobj} ajax call. then, when tell simple.py print out keys, result ["departments[two]", "departments[one]"] instead of expect, ["departments"]. then, since "departments" apparently not key, keyerror when try print json.dumps(fs["departments"].value), while print json.dumps(fs["departments[one]"].value) gives me result of 1.
according documentation fieldstorage, "the fields, accessed through form[key], instances of fieldstorage", thought fieldstorage object have "departments" key value fieldstorage object containing keys "one" , "two". however, doesn't seem true. how form multi-level javascript object , access in python script using fieldstorage?
this jquery being silly. should set traditional: true in $.ajax call.
Comments
Post a Comment