Kassy Coan Kassy Coan - 1 month ago 5
Javascript Question

Accessing data from Google App Engine datastore in a javascript file

What are the options for accessing information from Google App Engine datastore in a javascript file?

BACKGROUND:

I am running an app on Google App Engine which uses the Google Chart Tools API. All the data which I would like to graph is in my Google App Engine datastore.

WHAT I'VE TRIED

I can access the data within my python script. Also, in the python script, I encode the data to JSON. My idea was to save this in a js file, which I could import into the HTML, then build the chart there. However, this does not work, because writing to local files is not supported in App Engine.

Answer

XMLHttpRequest

From the python file: Write the JSON data to a URL. In App Engine, this is done with

self.response.write(data)

From the Javascript, make the XHR request like this:

function getData() {
    var dataObject = new XMLHttpRequest();
    dataObject.open('GET', 'URL', true);
    dataObject.send();
    return dataObject;
}

This will return a XMLHttpRequest object with the data in a string in the response or responseText attribute.