You need to give the input a name
attribute if you want its data to be submitted in the form.
Python cgi.FieldStorage() is empty on form submission
문제
I have the following directives in my Apache 2 virtual host configuration:
<Directory /var/www/foo_test>
Options MultiViews +ExecCGI
AddHandler cgi-script .py
AllowOverride All
</Directory>
I have the following simple form HTML:
<html>
<body>
<form action="foo.py" method="post" id="foo_form">
<input type="text" id="foo" />
<input type="submit" />
</form>
</body>
</html>
Here is foo.py
, the executable script that is called when the form is submitted:
#!/usr/bin/env python
import cgi
arguments = cgi.FieldStorage()
print "Content-Type: text/html\n"
print repr(arguments)
When I submit the form, I get the following response:
FieldStorage(None, None, [])
There is nothing in the FieldStorage
object instance that contains values for the fields specified in the form foo_form
.
If I replace the Python script with a Perl script and its CGI
module, along with changing the Apache directives to handle Perl CGIs, I have no problems reading fields out of the form. This suggests to me that my Apache setup is okay.
However, am I missing something with instantiating the cgi.FieldStorage()
object, or with my Apache setup, that is causing the form submission to fail?
해결책
다른 팁
It blew my mind, but this is what happened to me.
Apparently you cannot "pull" the information from cgi.FieldStorage
more than once ?
Run from any console
tab in a browser:
$.post('/api/save.cgi', {'hello': 'world'}, (response) => {
console.log("DONE");
});
Python code in backend
#!/usr/bin/env python3
import cgi
fcgi = cgi.FieldStorage()
sys.stderr.write("try 1 %s \n" % fcgi)
fcgi = cgi.FieldStorage()
sys.stderr.write("try 2: %s \n" % fcgi)
See error log:
try 1: FieldStorage(None, None, [MiniFieldStorage('hello', 'world')])
try 2: FieldStorage(None, None, [])