我正在尝试将一个简单的python脚本(这里我使用了一个可以正常工作的随机双单词生成器)放在带有Ajax的网页上的div中,并在它下面有一个重新加载它的按钮。页面成功加载脚本..。但是,我还没有完全理解缺少的部分来再次调用脚本来重新加载由python生成的随机的两个单词(我理解下面的代码为什么是错误的,但我无法想出如何使它正确!)指针很受欢迎!
(是的,我正在使用Python2.4,因为我的网络主机还没有升级--它们很快就会升级了!)是的,我看到了this的问题,但这对我没有用.)
HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Find a sentence</title>
<script src="http://code.jquery.com/jquery-2.0.3.js"></script>
<script>
$(function()
{
$.ajax({
url: "cgi-bin/test.py",
type: "GET",
data: {foo: 'bar', bar: 'foo'},
success: function(response){
$("#div").html(response);
}
});
});
</script>
</head>
<body>
<div id="div"></div>
<form name="input" action="cgi-bin/test.py" method="get">
<input type="submit" value="Regenerate!">
</form>
</body>PYTHON:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import linecache
from random import randrange
print "Content-Type: text/html"
print
# file length counter
def file_len(fname):
f = open(fname)
try:
for i, l in enumerate(f):
pass
finally:
f.close()
return i + 1
# range sizes
one_size = file_len('text/one.csv')
two_size = file_len('text/two.csv')
# random lines
one_line = randrange(one_size) + 1
two_line = randrange(two_size) + 1
# outputs
one_out = linecache.getline('text/one.csv', one_line)
two_out = linecache.getline('text/two.csv', two_line)
# output
sentence = one_out.strip('\n') + " " + two_out.strip('\n') + " "
print sentence发布于 2014-10-05 16:51:40
好吧,我假设你必须点击“重新生成”按钮才能重新加载表单。我的想法是将ajax逻辑放在一个单独的函数中。我认为您不需要表单,因为重新加载调用现在是ajax调用。
<head>
<script>
function reload() {
$.ajax({
url: "cgi-bin/test.py",
type: "GET",
data: {foo: 'bar', bar: 'foo'},
dataType : "html",
success: function(response){
$("#div").html(response);
}
});
}
$(window).load(function() {
reload();
$("#reload_button").click(function() {
reload();
});
});
</script>
</head>
<body>
<div id="div"></div>
<input type="button" id="reload_button" value="Regenerate!">
</body>https://stackoverflow.com/questions/26204806
复制相似问题