Flask ajax dosen’t show any info

  ajax, flask, python

I am trying to make a flask app that should render something from an ajax to a div, but it does not work.
This is what I have tried :
flask

@app.route('/preview')
def preview():
    return "wowowowowow"
                <script>
                    $(document).ready( function() {
                        $('#preview_button').click(function() {
                           $.ajax("{{ url_for('preview') }}").done(function (reply) {
                              $('#container').html(reply);
                           });
                        });
                    });
                </script>
                <input type="button" name="preview_button" id="preview_button" value="Preview" class="btn-sm btn-rounded"></input>

                <div id="container"></div>

( Flask jinja2 update div content without refresh page )
Another question is how can I send some data to the flask and render that.
For example :

                <input type="button" name="preview_button" id="preview_button" value="Preview" class="btn-sm btn-rounded"></input>
                <input value="some data" />
                <div id="container"></div>

and I want to send the "some data" to the flask function do something there and then return the result and rendering to the div without refreshing the page/redirecting, I tried something with forms but unsuccessfully.

Source: Python Questions

LEAVE A COMMENT