0

Suppose I have performed an AJAX call with jQuery in the following fashion:

key = 'boo'
$.ajax({
  type: 'GET',
  async: true,
  url: '/output',
  data: JSON.stringify({'location':key}),
  success: function(data) {
  }
});

I have a route in my Python App Engine code that receives the call on '/output', but how can I get it to access the data I've passed in the AJAX call? That is, how do I fill in the following:

class OutputRoute(webapp.RequestHandler):
  def get(self):
    # something goes here to get the data from above
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127

1 Answers1

2

Why are you JSON.stringifying your 'data' param? If you don't do that and instead write:

data: {'location': key},

Then in your handler you can just write:

location = self.request.get('location')

jQuery.ajax will take care of turning the object specified in the data parameter into query params (for a GET), and webapp.RequestHandler.request.get parses the query params.

Moishe Lettvin
  • 8,462
  • 1
  • 26
  • 40
  • Worked like a charm! Any suggestions what to do if the method isn't GET? I'm using PUTs here and there and am just forming the query params by hand there. – Chris Bunch Nov 08 '11 at 15:42
  • I'm pretty sure it will "just work" for PUTs -- the data field will be put in the header, and webapp will extract it from there instead of from the url query. – Moishe Lettvin Nov 08 '11 at 16:03
  • This should work fine even if he is calling `JSON.stringify` - the end result is the same either way. – Nick Johnson Nov 08 '11 at 23:34
  • I assumed that the JSON string itself ("{'location':'key_value'}") would be appended to the query or sent as POST/PUT data, instead of being changed into params, like "location=key_value". Is jQuery (or webapp) smart enough to make that part "just work" too? – Moishe Lettvin Nov 08 '11 at 23:40