I've created a page that interacts with an app written with Python on GAE, to return JSON data (via JSONP, because Cross-origin stuff). However, no matter what method I use, the page always hangs and data never actually makes it to the screen. It runs just fine if I request the stuff by typing the appspot URL into my address bar, though.
Here's the main part of le code.
Python main.py (on GAE)
def retrieve_data(self):
# Retrieve data from some API, manipulate and return result.
# Note: I'm not using this handler for the request.
# It's just there in case I need it.
class MainHandler(webapp2.RequestHandler):
def get(self):
data = retrieve_data(self)
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(
json.dumps(data)
)
# I'm using this handler for the JSONP request.
class JSONPHandler(webapp2.RequestHandler):
def get(self):
data = retrieve_data(self)
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(
"%s(%s)" %
(urllib2.unquote(self.request.get("callback")),
json.dumps(data))
)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/jsonp', JSONPHandler)
], debug=True)
index.js (Not hosted on GAE)
function add(data) {
// Sort data, add to DOM.
}
$.ajax({
type: "GET",
dataType: "jsonp",
url: "(APPSPOT URL)/jsonp",
success: function(data) { add(data) }
});
I've also tried $.get, creating a script tag with a src pointing to the appspot link, and other XMLHTTPRequest methods people described, but none seem to work. If I tell success to just console.log the data, it will do so after a few seconds of running.
So, is there something wrong with the code? Am I missing something in main.py, or am I AJAXing it wrong?