简体   繁体   中英

how to use Ajax with Python flask

I have written, AJAX script using Jquery. 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

$(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.

Here is the html code

<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 .

You are posting thruogh html submit form and also via AJAX .So if you want to POST via AJAX .Change type='button' like

<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"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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