有人可以向我解释我收到的错误吗?

问题描述:

我有一个CGI脚本,它导入cgi,创建一个存取器函数,然后尝试查找变量。功能是:有人可以向我解释我收到的错误吗?

cgi_form = cgi.FieldStorage() 

def get_cgi(field, default=''): 
    if cgi_form.has_key(field): 
     return cgi_form[field].value 
    else: 
     return default 

这可能没有必要。但是,当我尝试将它用于电子邮件时,我试图从XHR发送的一个字段,它错误了。代码触发问题的路线是:

sys.stderr.write('email: ' + get_cgi('email')) 

Apache的日志有:

[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/ 

,我试图效仿客户端代码:

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; 
    } 

上午我用JavaScript把适当的东西发送给XHR?为什么在Python中调试get_cgi('email')时出现“TypeError:not indexable”,我该怎么做才能纠正?

+2

您的请求标题不应设置为文本。它应该是:request.setRequestHeader(“Content-type”,“application/x-www-form-urlencoded”); – scrappedcola

+0

谢谢;我更改了内容类型,并解决了即时问题。现在,它提供了关于脚本头文件过早结束的错误,并且我想调查我的程序是否为该输入提供适当的输出。 – JonathanHayward

第一个问题是请求标题未设置为request.setRequestHeader("Content-type","application/x-www-form-urlencoded");。 对于过早的标题问题,它是相同的概念。在打印任何数据之前,请确保您正在打印内容标题。

print "Content-Type: text/html" 

通常我会把内容类型的打印输出放在我的代码的顶部,这样我就可以看到任何东西被发送回浏览器。这样,当你的代码错误发生时,你至少可以看到问题所在。