我试图凑齐一些代码。我试图将两种不同的功能合并为一个。通过Ajax发送给python的URL位置
代码是:
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<center>
# first function
<form id="search_box"><input id="search_text" type="text"/></form>
<br/>
<div id="jQuery_send"></div>
<div id="python_response"></div>
<script>
$('#search_box').submit(function() {
$('#jQuery_send').text("jQuery sent: " + $('#search_text').val());
$.ajax(
{
type: "POST",
url: "test2.py",
data: "stuff_for_python=" + $('#search_text').val(),
success: function(response)
{
$('#python_response').text("Python returned: " + response);
}
});
return false;
});
</script>
#second function
<p id="demo"></p>
<button onclick="urlLocation()">Click here to get URL of the page</p>
<script>
function urlLocation()
{
var loc = location.href;
document.getElementById("demo").innerHTML=loc;
}
</script>
</center>
</body>
</html>
的Python代码是:
#!/usr/bin/python
import cgi, cgitb
cgitb.enable() # for troubleshooting
form = cgi.FieldStorage()
jquery_input = form.getvalue("stuff_for_python", "nothing sent")
print "Content-type: text/html"
print
print jquery_input
对于功能1,无论是输入到搜索框中通过的jQuery/AJAX发送到python脚本,并呼应通过框下方的jQuery和python。
Function2使用一个按钮,单击它时使用JavaScript来打印它下面的当前URL。
我想知道的是,有没有办法点击一个按钮,然后获取当前URL(函数2)以相似的方式由jQuery和python(函数1)回显?我是编程新手,并试图弄清楚所有这些部分如何组合在一起。我意识到上面的代码本身并没有太大的作用,但是如果在将来的项目中进行定制,它可能会很有用。
Python 2.7.6以及Apache2正在我的测试环境中使用。