使用AJAX检索Python程序的输出
test.html使用AJAX检索Python程序的输出,python,ajax,Python,Ajax,test.html {% extends 'base.html' %} {% block extra_js %} <script language="Javascript"> function xmlhttpPost(strURL) { var xmlHttpReq = false; var self = this; // Mozilla/Safari if (window.XMLHttpRequest) { self.xmlHttp
{% extends 'base.html' %}
{% block extra_js %}
<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>
{% endblock %}
{% block main1 %}
<form name="f1">
<p>word: <input name="word" type="text">
<input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/site_media/rt.py")'></p>
<div id="result"></div>
</form>
{% endblock main1 %}
所以当我点击Go按钮时,我想在div标签中得到I值10。但结果是 def ret():i=10返回i
它只是读取rt.py并显示在网页上。你期望什么?字符串
10
是否显示在页面中
不会,因为您有以下问题:
def ret():
i=10
return i