gpt4 book ai didi

python - 为什么我的 POST 在获取值后不起作用

转载 作者:行者123 更新时间:2023-12-01 07:34:54 25 4
gpt4 key购买 nike

因此,我从使用 css 和 jQuery 的模板获取用户输入,然后我会在单击时将该数据发回。但是,我无法访问数据,因为我相信它没有被发回。

我相信我的ajax代码无法识别点击,有人可以帮助我吗?我花了一整天的时间试图弄清楚我做错了什么。

#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)

这是 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>

最佳答案

我对 multidict 的 request.forms 类型有问题,所以我转换为 dict,这解决了我的所有问题。我通常将 post 和 string URL 对象合并为一个,只是为了使其更加灵活。

def merge_dicts(*args):
result = {}
for dictionary in args:
result.update(dictionary)
return result

payload = merge_dicts(dict(request.forms), dict(request.query.decode()))

请记住,该 Bottle 使用帖子中表单元素的名称而不是id。因此,请确保所有输入都有一个名称

(我在你的示例中没有看到)

关于python - 为什么我的 POST 在获取值后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57031565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com