I have a CGI script that imports cgi, makes an accessor function, and then tries to look up variables. The function is:
cgi_form = cgi.FieldStorage()
def get_cgi(field, default=''):
if cgi_form.has_key(field):
return cgi_form[field].value
else:
return default
Which is probably not necessary. But when I try to use it for email, one of the fields I attempted to send from an XHR, it errors out. The line of code triggering the problem is:
sys.stderr.write('email: ' + get_cgi('email'))
The Apache log has:
[Wed Aug 29 11:25:33 2012] [error] [client ::1] Traceback (most recent call last):, referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] File "/Users/jonathan/mirror/professional/calendar-todo/create_account.cgi", line 26, in <module>, referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] sys.stderr.write('email: ' + get_cgi('email')), referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] File "/Users/jonathan/mirror/professional/calendar-todo/create_account.cgi", line 21, in get_cgi, referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] if cgi_form.has_key(field):, referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cgi.py", line 580, in has_key, referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] TypeError: not indexable, referer: http://localhost/professional/calendar-todo/
[Wed Aug 29 11:25:33 2012] [error] [client ::1] Premature end of script headers: create_account.cgi, referer: http://localhost/professional/calendar-todo/
The client-side code that I was trying to emulate is:
document.getElementById('create_account_button').onclick = function()
{
var request = new XMLHttpRequest();
request.open('POST', 'create_account.cgi');
request.setRequestHeader('Content-Type', 'text/plain');
request.send('email=' + encodeURIComponent(document.getElementById('create_email').value) + '&password=' + encodeURIComponent(document.getElementById('create_password').value) + '&password_hint=' + encodeURIComponent(document.getElementById('create_password_hint').value));
load_from_request(request);
return false;
}
Am I sending things appropriately to the XHR in JavaScript? Why, in Python, is the debugging call to get_cgi('email') giving a "TypeError: not indexable" and what can I do to correct that?