XMLHttpRequest调用后端python cgi脚本,python脚本没有响应

时间:2017-03-23 22:07:23

标签: javascript python html forms cgi

我用表单数据编写了一个简单的html文件,并使用XMLHttpRequest将数据发布到另一个python cgi脚本,但由于某种原因,python脚本没有按预期执行。在将数据从html传递给python之后,cgi脚本应该创建一个文本文件并在该文本文件中写入一行,但是在我点击“运行”后不会创建文本文件。按钮。知道为什么会这样吗?这是html代码和python脚本:

---- ---- HTML

<html>
<head> <title> TEST </title> </head>
<script type="text/javascript">
    function run(){
        var facPath = document.forms["inputParams"]["FacPath"].value;
        var facId   = document.forms["inputParams"]["IdName"].value;

        var xhttp; 
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        } else {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("response").innerHTML = this.responseText;
        }
        };
        xhttp.open("POST", "/cgi-bin/processRequest.py", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-
        urlencoded");
        xhttp.send("facPath="+facPath+"&facId="+facId);
    }
</script>

<body>
<P>
<form name="inputParams" >
Factor File Path <BR>
<input type="text" name="FacPath"> <BR><BR>

Factor ID Name <BR>
<input type="text" name="IdName"> <BR><BR>

<input type="button" value="Run" name="submit" onclick="run()">
</form>
</P>
<div id="response"> </div>
</body>
</html>

---- Python cgi脚本----

import cgi
import sys

form = cgi.FieldStorage()
facPath = form.getvalue('facPath','None')
if facPath:
    with open(r'C:\Users\XXX\inputSpec.txt','w') as outputFile:
        outputFile.write(facPath)

sys.stdout.write("Status: 200 OK\n")
sys.stdout.write("Content-Type: text/plain\n")
sys.stdout.write("\n")
sys.stdout.write("Success.")

我是否需要使python脚本可执行?还是其他原因?谢谢。

0 个答案:

没有答案