A
A
Alexey Samoilov2015-10-20 12:04:55
JavaScript
Alexey Samoilov, 2015-10-20 12:04:55

How to load table using ajax jquery Flask Python?

Good afternoon.

Flask

#У меня есть функция find, которая возвращает json (в данном случае с одним значением)

@app.route('/_find')
def find():
    req = request.args.get('req', 0, type=str)
    print(req)
    return jsonify(result=req+'!')

@app.route('/')
def index():
    return render_template('test.html')


A form that executes a function and outputs the result from json'a
HTML
<script type=text/javascript>
            $(function() {
              $('#find').bind('click', function() {
                $.getJSON('/_find', {
                  req: $('input[name="req"]').val()
                }, function(data) {
                  $("#res").text(data.result);
                });
                return false;
              });
            });
        </script>
    </head>
    <body>
        <div class="container">
               <div>
                <form>
                    <p>
                        <input type="text" size="5" name="req">
                        <span id="res"> </span>
                    </p>
                    <a href="javascript:void();" id="find">find</a>
                </form>
            </div>


How to make the function return json with several values:
{results:['test1', 'test2', 'test3']}

The js function takes these values ​​and outputs them as a table or list
<ul>
<li>test1</li>
<li>test2</li>
<li>test3</li>
</ul>


Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Samoilov, 2015-10-21
@tibhar940

pcdesign : Thanks for the help!
In this case, it is incorrect to indicate something as a solution.
Therefore, I will write the solution myself - if you do not know the PL - it is better to study its basics first - and then ask questions.
Unfortunately, JavaScript is very different from Python - I don’t know if this is good or bad, and I couldn’t get into it right away, and therefore I was tempted to write to the Toaster.
PS - I will delete the question in the near future.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question