當我嘗試將數據發佈到我的CGI文件時,我的CGI文件顯示實際的發佈數據無效。我在前端使用HTML/JavaScript,在後端使用Python。使用XMLHttpRequest將數據發佈到CGI文件會導致BadHeader
作品:
<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,
我沒有得到這個錯誤時我是這樣做的第一種方式,但第二種方法是給我這個錯誤。我需要它工作的第二種方式。