gpt4 book ai didi

javascript - 有人可以向我解释我遇到的错误吗?

转载 作者:行者123 更新时间:2023-12-02 19:24:31 25 4
gpt4 key购买 nike

我有一个 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:不可索引”,我该如何纠正该问题?

最佳答案

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

print "Content-Type: text/html" 

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

关于javascript - 有人可以向我解释我遇到的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com