简体   繁体   English

遍历表单以通过jQuery / AJAX将动态数据提交到Flask

[英]Iterating over form to submit dynamic data to flask via jQuery/AJAX

I'm trying to create a web app with flask that has rows of data obtained from API calls. 我正在尝试使用烧瓶创建一个Web应用程序,该烧瓶具有从API调用获取的数据行。 Currently I have a column called 'Ticket Number' that contains a set of numbers. 目前,我有一个名为“票号”的列,其中包含一组数字。 My desired functionality is for the user to click on a number and have a modal present more specific information (via a separate python API call). 我想要的功能是让用户单击一个数字,并有一个模态呈现更具体的信息(通过单独的python API调用)。 I'm using AJAX to send the data to my flask app to prevent a page refresh on click. 我正在使用AJAX将数据发送到我的flask应用程序,以防止单击时刷新页面。 The only problem is that no matter which button I click, it thinks I am clicking the first button listed and gives me the results for the first number. 唯一的问题是,无论我单击哪个按钮,它都认为我正在单击列出的第一个按钮,并为我提供第一个数字的结果。 Below I've posted the Python, jQuery, and html. 在下面,我发布了Python,jQuery和html。 Any and all help is greatly appreciated, thanks for your time. 非常感谢您提供的所有帮助。

jQuery: jQuery的:

$().ready(function(){
    $('button').click(function() {
        $.ajax({
            url: '/ticket',
            data: $('form').serialize(),
            type: 'POST',
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                console.log(error);
            }
        });
    });
});

Flask: 烧瓶:

@app.route('/ticket', methods=['POST', 'GET'])
def ticket_drill():

    ticket_number = request.form['ticketNumber']

    header = {
        'authorization': "super_secret_key",
        'content-type': "application/json",
        'cache-control': "no-cache"
    }
    s = requests.get("https://my_site/v4_6_release/apis/3.0/service/tickets/" + 
    str(ticket_number), headers=header)
    ticket_details = json.loads(s.text)
    ticket_id = ticket_details['id']

    return json.dumps({'status':'OK','Ticket Number:':ticket_id});

HTML: HTML:

{% for ticket_number in service_board %}
            <tr>
                    <td>
                        <form class="form-signin" action="/serviceboard" method="post" role="form">
                            <button class="btn btn-lg btn-primary btn-block" type="button">{{ticket_number}}
                                <input type="hidden" name="ticketNumber" value="{{ticket_number}}" />
                            </button>
                        </form>
                    </td>    
    </tr>
{% endfor %}

I would give your button an id and call that with the jquery like so: 我会给您的按钮一个ID,并使用jQuery进行调用,如下所示:

$().ready(function(){
$('button#ticketNumberButton').on('click',function() {
    var ticketNumberVar = $(this).parent().find(' form ').find('#ticketNumber');

    $.ajax({
        url: '/ticket',
        data: $('form').serialize(),
        type: 'POST',
        success: function(response) {
            console.log(response);
            obj = JSON.parse(response);
            ticketNumberVar.val(obj.Ticket_Number);
        },
        error: function(error) {
            console.log(error);
        }
    });
});

}); });

Maybe this is just me, but I would get rid of the space and ";" 也许这只是我,但我会摆脱空间和“;” in your view as well. 在您看来也是如此。

return json.dumps({'status':'OK','Ticket_Number:':ticket_id})

HTML: HTML:

{% for ticket_number in service_board %}
        <tr>
                <td>
                    <form class="form-signin" action="/serviceboard" method="post" role="form">
                        <input type="hidden" id="ticketNumber" name="ticketNumber" value="{{ticket_number}}" />

                    </form>
                    <button id="ticketNumberButton" class="btn btn-lg btn-primary btn-block" type="button">{{ticket_number}}</button>

                </td>    
</tr>

{% endfor %} {%endfor%}

Hopefully that helps, good luck! 希望有帮助,祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM