简体   繁体   English

如何在Python Flask中使用Ajax

[英]how to use Ajax with Python flask

I have written, AJAX script using Jquery. 我已经使用Jquery编写了AJAX脚本。 When I debug through the script, I noticed that it doesn't go through 'success' and 'error' but it exists. 当我调试脚本时,我注意到它没有通过“成功”和“错误”,但是确实存在。

Here is the JQuery code 这是JQuery代码

$(document).ready(function(){

    $('#report-submit').click(function() {
        alert('inside click');

        $.ajax({

            url: '/admin/file/start/rerun_reports/',
            //data: $i('form').serialize(),
            type: 'POST',
            success: function(response) {
                //alert(response);
                console.log("it works..................");
                console.log(response);
                $("#log-results").html(response);
            },
            error: function(error) {
                console.log(error);
            }
        });
    });

});

However, when I submit the the button, I get the data to browser without Ajax. 但是,当我提交按钮时,我没有Ajax就将数据发送到浏览器。

Here is the html code 这是HTML代码

<form method="POST" action="/admin/file/start/rerun_reports/">
                <input name="_csrf_token" type=hidden value="{{ csrf_token() }}">
                <input class="btn btn-danger btn-lg" id="report-submit"
                    type="submit" value="submit">

            </form>

Here I send the data to browser 在这里我将数据发送到浏览器

return render_template('tools.html', data = json.dumps(start.logdata))

When print the status code. 打印状态码时。 I get the 403 error . 我收到403 error

You are posting thruogh html submit form and also via AJAX .So if you want to POST via AJAX .Change type='button' like 您张贴thruogh html submit form ,并通过AJAX 。所以,如果你想POST通过AJAX .Change type='button'一样

<input class="btn btn-danger btn-lg" id="report-submit"
                type="submit" value="submit">

to

<input class="btn btn-danger btn-lg" id="report-submit"
                type="button" value="submit"> 

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

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