如何在Python烧瓶中使用Ajax

时间:2015-04-02 16:31:49

标签: jquery python ajax flask

我已经使用Jquery编写了AJAX脚本。当我通过脚本进行调试时,我注意到它没有经历“成功”和“错误”,但它存在。

这是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);
            }
        });
    });

});

但是,当我提交按钮时,我将数据传输到没有Ajax的浏览器。

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

这里我将数据发送到浏览器

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

打印状态代码时。我得到403 error

1 个答案:

答案 0 :(得分:0)

您正在发布thruogh html submit form以及AJAX。因此,如果您想POST通过AJAX。请更改type='button'

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

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