【发布时间】:2014-06-14 20:00:30
【问题描述】:
当我尝试将数据发布到我的 CGI 文件时,我的 CGI 文件显示实际发布的数据无效。我在前端使用 HTML/JavaScript,在后端使用 Python。
作品:
<form name="login" action="/cgi-bin/register.py" method="POST">
Username:<input type="text" name="username"><br>
Password:<input type="password" name="password"><br>
Confirm password:<input type="password" name="confirmpassword"><br>
</form>
但是,这会导致页面刷新。我试图避免这种情况并在同一页面内显示文本(无需重新加载)。因此,我选择使用 XMLHTTPRequest 来异步处理这个事件。
这就是我想要实现的目标:
<script>
function validateLogin()
{
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username.length <= 0 || password.length <= 0)
{
document.alert("The username or password cannot be blank");
return;
}
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("resultText").innerHTML=xmlhttp.responseText;
}else if (xmlhttp.readyState==4) {
document.write(xmlhttp.status + xmlhttp.statusText);
}
}
xmlhttp.open("POST","/cgi-bin/login.cgi",true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
xmlhttp.send("username=" + username + "&password=" + password);
}
</script>
CGI 文件:
#!/usr/bin/python
import cgi
from dbmanager import openConnection
from passlib.hash import sha256_crypt
s = "Content-type: text/html\n\n\n"
form = cgi.FieldStorage()
username = form["username"].value
password = form["password"].value
message = None
我在 python 中遇到错误,声明 Bad header=FieldStorage(None, None,
当我第一种方式执行此操作时,我没有收到此错误,但第二种方式给了我此错误。我需要它以第二种方式工作。
【问题讨论】:
标签: javascript python html cgi