【发布时间】:2012-10-17 09:38:47
【问题描述】:
我正在尝试弄清楚如何将 jQuery AJAX 与 Python 的 Bottle 模块一起使用。
这是我最初的尝试,但它不工作,我真的碰壁了(整天编程,我的大脑有点炸):
from bottle import route, request, run, get
@route('/form')
def construct_form():
return '''
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(e) {
$.ajax({
type: 'POST',
url: '/ajax',
data: $(this).serialize(),
success: function(response) {
$('#ajaxP').html(response);
}
});
e.preventDefault();
});
});
</script>
</head>
<body>
<form method="POST" action="/ajax">
<input id="ajaxTextbox" name="text" type"text" />
<input id="ajaxButton" type="button" value="Submit" />
</form>
<p id="ajaxP">Nothing to see here.</p>
</body>
</html>
'''
@route('/ajax', method='POST')
def ajaxtest():
theText = request.forms.text
if theText:
return theText
return "You didn't type anything."
run(host = 'localhost', port = '8080')
基本上,我想做的就是在文本框中输入文本,单击按钮,然后将“ajaxP”的innerHTML更改为输入的文本。
没有错误,当我按下按钮时它什么也没做。
有什么帮助吗?
已解决:表单按钮的类型必须是“提交”,而不是“按钮”。掌心。
【问题讨论】:
-
错误是什么?在例如 Chrome 中打开开发者控制台以找到它
-
哎呀,对不起。
Failed to load resource: Resource failed to load file:///F:/ajax