Search code examples
jquerytornado

getJson not returning file content


I have this jquery request, jsonView is open sourced way of pretty printing a json string:

$('#element2').jsonView($.getJSON("snapshot1.json",function(result){
        return result;
    }));

And this tornado server-sided code:

class JsonHandler1(tornado.web.RequestHandler):
    def get(self):
        print("SENT SNAPSHOT1")
        self.render("snapshot1.json")

However, this returns

{
"readyState": 1,
"getResponseHeader": ,
"getAllResponseHeaders": ,
"setRequestHeader": ,
"overrideMimeType": ,
"statusCode": ,
"abort": ,
"state": ,
"always": ,
"catch": ,
"pipe": ,
"then": ,
"promise": ,
"progress": ,
"done": ,
"fail":}

Instead of the contents in the snapshot1.json file. I get 'SENT SNAPSHOT' in my server sided console. So I'm sure it is definitely being called, and the file is in the same directory as well.

What else am I doing wrong??


Solution

  • $.getJSON doesn't return the JSON data - since it returns asynchronously before the data has necessarily been retrieved, it can't. You need to consume the data in the success function.

    EG

    $.getJSON("snapshot1.json",function(result){
            $('#element2').jsonView(result);
    });