使用XMLHttpRequest向CGI文件发送POST数据导致BadHeader

10 投票
2 回答
3072 浏览
提问于 2025-04-18 04:41

当我尝试向我的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,

当我用第一种方式时没有这个错误,但第二种方式却给我带来了这个错误。我需要第二种方式能够正常工作。

2 个回答

-1

JavaScript没有正确发送值,是因为HTML元素的属性设置不对。

试着把元素的name属性改成id属性,像你的JavaScript代码里要求的那样,元素包括usernamepassword

结果应该像这样:

<form name="login" action="/cgi-bin/register.py" method="POST"> 用户名:<input type="text" id="username"><br> 密码:<input type="password" id="password"><br> 确认密码:<input type="password" id="confirmpassword"><br> </form>

4

对于回显服务器:

HTML:

<html>
 <head>

 <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","../post_test.py",true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
xmlhttp.send("username=" + username + "&password=" + password);
}
</script>
 </head>




 <body>


<form name="login" >
Username:<input type="text"  id="username"><br>
Password:<input type="text"  id="password"><br>
Confirm password:<input type="text"  id="repassword"><br>

</form>
<button onclick="validateLogin()">Login</button>
<span id="resultText"></span>
</body>
</html>

CGI脚本:

#!/usr/bin/python2.7

import cgi


form = cgi.FieldStorage()
print "Content-Type: text/html;charset=utf-8"
print "Access-Control-Allow-Origin:*"
print
print form

把输入框的类型从 password 改成 text,因为这样会有安全漏洞!

你的CGI脚本返回了错误的答案。谁知道服务是不是在线呢?所以需要一些类型、状态、头信息、内容等等。

检查帖子地址:..// 代表 当前网址 + 新路径 + 目标

在JavaScript中:通过ID调用,但ID参数在哪里呢?

撰写回答