使用XMLHttpRequest将数据发布到CGI文件会导致BadHeader [英] POST data to CGI file using XMLHttpRequest causes BadHeader

查看:146
本文介绍了使用XMLHttpRequest将数据发布到CGI文件会导致BadHeader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将数据发布到我的CGI文件时,我的CGI文件显示实际的发布数据无效。我在前端使用HTML / JavaScript,在后端使用Python。

Works:

 < form name =loginaction =/ cgi-bin / register.pymethod =POST> 
用户名:< input type =textname =username>< br>
密码:< input type =passwordname =password>< br>
确认密码:< input type =passwordname =confirmpassword>< br>
< / form>

但是,这会导致页面刷新。我试图避免这种情况,并在同一页面中显示文本(无需重新加载)。因此,我选择使用XMLHTTPRequest来异步处理这个事件。



这就是我想要实现的:

 <脚本> 
函数validateLogin()
{
var username = document.getElementById(username)。value;
var password = document.getElementById(password).value;

if(username.length< = 0 || password.length< = 0)
{
document.alert(用户名或密码不能为空) ;
return;
}

var xmlhttp;

if(window.XMLHttpRequest){//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
} else {//代码为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 +&密码=+密码);
}
< / script>

CGI档案:

 #!/ usr / bin / python 

从dbmanager导入cgi
从passlib.hash导入openConnection
导入sha256_crypt

s =Content-type:text / html\\\
\\\
\\\


form = cgi.FieldStorage()

username = form [username] .value
password = form [password]。value
message = None

我在python中遇到一个错误,指出 Bad header = FieldStorage(None,None,



当我以第一种方式做这件事时,会得到这个错误,但第二种方法是给我这个错误,我需要它以第二种方式工作。

解决方案

对于echo服务器:



HTML:



<$ p $















$ username = document.getElementById(username)。value;
var password = document。 。的getElementById( 密码)值;

if(username.length< = 0 || password.length< = 0)
{
document.alert(用户名或密码不能为空) ;
return;
}

var xmlhttp;

if(window.XMLHttpRequest){//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
} else {//代码为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 +&密码=+密码);
}
< / script>
< / head>




< body>


< form name =login>
使用者名称:< input type =textid =使用者名称>< br>
密码:< input type =textid =password>< br>
确认密码:< input type =textid =repassword>< br>

< / form>
< button onclick =validateLogin()>登入< / button>
< span id =resultText>< / span>
< / body>
< / html>



CGI-SCRIPT:



 #!/ usr / bin / python2.7 

import cgi


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

将输入类型密码替换为文本因为有安全漏洞!



哟在cgi脚本中出错了。谁知道服务是活的?所以需要一些类型,状态,标题,内容。

查看帖子地址: ..// mean currient_uri + new_path + target



关于javascript:通过ID调用,但ID参数在哪里? b

When I try posting data to my CGI file, my CGI file says the actual post data is invalid. I am using HTML/JavaScript for the front end and Python for the backend.

Works:

<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>

However, this causes the page to refresh. I am trying to avoid this and have text display within the same page(without reloading). Hence, I have chosen to use an XMLHTTPRequest to asynchronously process this event.

This is what I want to achieve:

<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 File:

#!/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

I am getting an error in python stating Bad header=FieldStorage(None, None,

I don't get this error when I do it the first way, but the second way is giving me this error. I need it to work the second way.

解决方案

For echo Server :

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-SCRIPT:

#!/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

Replace input type password to text because got security bugs !

Yo got wrong answer on cgi script. Who know service is live ? So need some type, status, header, content..

Check post address : ..// mean currient_uri + new_path + target

On javascript: Call by ID but where ID parameter ?

这篇关于使用XMLHttpRequest将数据发布到CGI文件会导致BadHeader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆