简单的Python AJAX示例无法运行

2024-06-06 15:49:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个简单的AJAX示例,基于此页面上的演示: http://www.degraeve.com/reference/simple-ajax-example.php

我复制了HTML部分并将其命名为ajax_演示.html. 例如:

<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
</script>
</head>
<body>
<form name="f1">
  <p>word: <input name="word" type="text">
  <input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/cgi-bin/simple-ajax-example.py")'></p>
  <div id="result"></div>
</form>
</body>
</html>

上面没有显示我的简单ajax的完整真实路径-示例.py这里:

<input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/cgi-bin/simple-ajax-example.py")'>

这两个文件都在我的apache服务器上。例如:

^{pr2}$

我的Python脚本在直接调用时可以工作,如下所示:

import cgi

form = cgi.FieldStorage()
secret_word = form.getvalue('word')

print "Content-type: text/html"
print ""
print "<p>The secret word is", secret_word, "<p>"

问题是,这根本不起作用。在ajax中_演示.html文本框中,当我输入文本并单击Go时,似乎什么都没有发生。在

我错过了什么?在


Tags: selfformifvalueexamplevarhtmltype