Python 为什么我的帖子在获得价值后不起作用
因此,我从一个使用css和jQuery的模板中获取用户输入,然后我会在点击后将这些数据发回。但是,我无法访问数据,因为我认为数据没有被发回 我相信我的ajax代码无法识别点击,请有人帮助我。我花了一整天的时间想弄清楚我做错了什么Python 为什么我的帖子在获得价值后不起作用,python,jquery,bottle,Python,Jquery,Bottle,因此,我从一个使用css和jQuery的模板中获取用户输入,然后我会在点击后将这些数据发回。但是,我无法访问数据,因为我认为数据没有被发回 我相信我的ajax代码无法识别点击,请有人帮助我。我花了一整天的时间想弄清楚我做错了什么 #This is main code from bottle import run, route, request, template, static_file, post def main(): #This is main code @route(
#This is main code
from bottle import run, route, request, template, static_file, post
def main():
#This is main code
@route('/assets/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='./Gui/assets')
@route('/')
def home():
return template('indexTest')
@post('/home')
def home():
radius=request.forms['radius']
noTr=request.forms['noOdTurbines']
result=radius+noTr
return ({'result': result})
if __name__ == '__main__':
main()
run(host='localhost', port=8080, debug=True)
#这是主代码
从瓶子导入运行、路由、请求、模板、静态文件、post
def main():
#这是主代码
@路由(“/assets/”)
def服务器_静态(文件路径):
返回静态_文件(文件路径,根='./Gui/assets')
@路由(“/”)
def home():
返回模板('indexTest')
@post(“/home”)
def home():
radius=请求。表单['radius']
noTr=request.forms['noOdTurbines']
结果=半径+noTr
返回({'result':result})
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
运行(host='localhost',port=8080,debug=True)
这是jQuery代码
<!DOCTYPE html>
<html>
<head>
<title>AJAX Example</title>
<script type="text/javascript">
$(document).ready(function(){
$('form').on('submit',(function(event){
$.ajax({
type: "POST",
url: "/home"
data: {
radius: $('#radius').val(),
noOfTurbines: $('#noOfTurbines').val()
},
error:function(){
alert('Error: something is wrong with values!!')
}
})
event.preventDefault();
});
}));
</script>
</head>
<body>
<form method="POST" action="/home">
<div id="exampleAccount" role="tabpanel">
<div id="exampleAccountForm">
</div>
<div>
<label for="radius">Radius of Swept-Area of Wind turbine:
</label>
<input type="text" id="radius" required>
</div>
<div >
<label for="noOfTurbines">Number of Wind turbines: </label>
<input type="text" id="noOfTurbines" required>
</div>
</div>
</div>
<div >
<button type="submit" class="btn btn-default"
id="submit">Finish</button>
</div>
</form>
</body>
</html>
AJAX示例
$(文档).ready(函数(){
$('form')。在('submit'),(函数(事件){
$.ajax({
类型:“POST”,
url:“/home”
数据:{
半径:$(“#半径”).val(),
中午城市:$(“#中午城市”).val()
},
错误:函数(){
警报('错误:值有问题!!')
}
})
event.preventDefault();
});
}));
风机扫掠区半径:
风力涡轮机的数量:
完成
我对request.forms类型的multidict有问题,所以我转换为dict,这解决了我所有的问题。我通常将post和stringurl对象合并为一个对象,以使其更灵活
def merge_dicts(*args):
result = {}
for dictionary in args:
result.update(dictionary)
return result
payload = merge_dicts(dict(request.forms), dict(request.query.decode()))
请记住,这个瓶子使用的是post中表单元素的名称
而不是id
。因此,请确保所有输入都有一个名称
(在您的示例中我没有看到)瓶子是否可能不支持此功能?在ajax代码中添加一个警报,查看它是否被击中。我在模板中没有看到
元素。@JohnGordon代码太大了,我只包含了相关部分。你说得对,不过我应该把它包括进去。我想我的问题是瓶子的要求。我通常使用request.forms.get(),但这次我使用的是字典。所以我使用了request.forms['first'],但它不起作用。我现在收到了“return self.dict[key][-1]”这个错误。@Programnik我收到了,但我认为问题并没有发帖。在使用request.forms发布数据后,我正在努力检索数据