为什么我的帖子在获得价值观后就不起作用了

2024-03-29 06:01:12 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我从一个使用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>

Tags: dividhomereturnismainrequestdef
1条回答
网友
1楼 · 发布于 2024-03-29 06:01:12

我对这个问题有意见申请表类型的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中使用的是form元素的name,而不是id。所以确保所有的输入都有一个name。你知道吗

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

相关问题 更多 >