我在使用Bottle框架獲得AJAX通信的一些問題。這是我第一次使用AJAX,所以我可能只是基本錯誤。希望Bottle/AJAX大師能夠將這個新手指向正確的方向。下面是我使用的代碼:使用Bottle(Python)的AJAX提交表單
#!/usr/bin/env python
from bottle import route, request, run, get
# Form constructor route
@route('/form')
def construct_form():
return '''
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
xmlhttp = new XMLHTTPRequest();
xmlhttp.onReadyStateChange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("responseDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "/ajax", true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input name="username" type="text"/>
<input type="button" value="Submit" onclick="loadXMLDoc()"/>
</form>
<div id="responseDiv">Change this text to what you type in the box above.</div>
</body>
</html>
'''
# Server response generator
@route('/ajax', method='GET')
def ajaxtest():
inputname = request.forms.username
if inputname:
return 'You typed %s.' % (inputname)
return "You didn't type anything."
run(host = 'localhost', port = '8080')
我在Chrome中測試,所以不需要使用特定於IE的代碼 - 萬一有人想到這個問題。 – patrickn 2012-02-06 20:09:06