【问题标题】:POST data to CGI file using XMLHttpRequest causes BadHeader使用 XMLHttpRequest 将数据发布到 CGI 文件会导致 BadHeader
【发布时间】: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


    【解决方案1】:

    由于错误的 HTML 元素属性,JavaScript 不会发布正确的值。

    尝试按照 JavaScript 代码的要求将元素 usernamepasswordname 属性更改为 id 属性。

    结果会是这样的:

    <form name="login" action="/cgi-bin/register.py" method="POST"> Username:<input type="text" id="username"><br> Password:<input type="password" id="password"><br> Confirm password:<input type="password" id="confirmpassword"><br> </form>

    【讨论】:

      【解决方案2】:

      回显服务器:

      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 脚本上得到了错误的答案。谁知道服务是实时的?所以需要一些类型、状态、标题、内容..

      查看发帖地址:..//意思是currient_uri + new_path + target

      在 javascript 上:通过 ID 调用但 ID 参数在哪里?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 2018-12-09
        • 2015-11-05
        • 1970-01-01
        • 2016-05-15
        相关资源
        最近更新 更多