2014-04-29 83 views
9

當我嘗試將數據發佈到我的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,

我沒有得到這個錯誤時我是這樣做的第一種方式,但第二種方法是給我這個錯誤。我需要它工作的第二種方式。

回答

-1

由於錯誤的HTML元素屬性,JavaScript不會發布正確的值。

嘗試將元素usernamepasswordname屬性更改爲id屬性,如您的JavaScript代碼所請求的屬性。

其結果將是這樣的:

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

3

回聲服務器:

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參數調用?

相關問題