Can't get json through ajax and flask
Question: I am trying to make a simple web application for logs show. On the web side:
python 3.4 and flask and on the client side it's simple web form with ajax.

Flask:

    
    
    import json
    from flask import Flask, jsonify, render_template, request
    
    app = Flask(__name__)
    
    @app.route('/json_test', methods=['GET'])
    def json_test():
        return open('log.json').read()
    
    
    @app.route('/')
    def index():
        return render_template('layout.html')
    
    if __name__ == '__main__':
        app.run(debug=True)
    

My HTML form

    
    
    <!DOCTYPE html>
    <script type=text/javascript src="{{
      url_for('static', filename='jquery.js') }}"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="{{
      url_for('static', filename='jquery.js') }}">\x3C/script>')</script>
    
    <script type=text/javascript>
      $LOG = {{ request.script_root|tojson|safe }};
    </script>
    
    
    <script type=text/javascript>
        $(function() {
            $('a#log').bind('click', function() {
                $.getJSON($LOG + '/json_test',
                    function(data){
                        $("#logs").text(data.result);
                        });
                        return false;
                });
      });
    </script>
    
    
    <p>
        <span id=logs>Logs should be here</span>
        <a href=# id=log>take log</a>
    </p>
    </html>
    

And my JSON example:

    
    
    {
    "data":
    {
        "misc":
        [
            {
                "name" : "JSON 1",
                "type" : "1"
            },
    
            {
                "name" : "JSON 2",
                "type" : "2"
            }
        ]
    }
    }
    

I wanna send a part of logs every 5 second. For this, I plan to use js
function: setInterval. Am I right?


Answer: Your JSON data has no `result` key. You have a `data` key instead.

Placing the `data.data` result in the `<span>` will only insert the text
`[object Object]` however; you need to insert actual text. You could turn the
object back to JSON text perhaps, using `JSON.stringify()`:

    
    
    $("#logs").text(JSON.stringify(data.data));
    

or you could send something more meaningful instead.

