2

Apologies up front for the noob question...

Hello, how do I get data from the Python end of an appengine server using jQuery.ajax? I know how to send data to the server using ajax and an appropriate handler, but I was wondering if someone could tell me what the ajax request for getting values from the server looks like. (Suppose I wanted to get a number from the datastore and use it in the javascript).

client sending to server (using jquery)

client-side javascript:

//jQuery and ajax function loaded.

<script type="text/javascript">
    var data = {"salary":500};
    $.ajax({
    type: "POST",
    url: "/resultshandler",
    data: data
</script>

server-side:

class ResultsHandler(webapp.RequestHandler):
    def get(self):
        n = cgi.escape(self.request.get('salary'))
        e = Engineer(salary = n)
        e.put()

and under def main():, I have the handler ('/put_in_datastore', ResultsHandler)

Again, what would be the similar code for retrieving numbers from the Python end? If someone could provide both the handler code and javascript code that would be great...

ejang
  • 3,982
  • 8
  • 44
  • 70

1 Answers1

9

The mechanism is exactly the same either way the data is flowing. Use the success parameter on the ajax call to operate on the data after the request successfully finishes. This is generally called a callback. Other callbacks exist. See http://api.jquery.com/jQuery.ajax/ for the complete information.

$.ajax({
  url: "/resultshandler",
  type: 'POST',
  data: data,
  success: function(data, status){
    //check status
    //do something with data
  }
});

On the Python end, you return data with self.response.write.out(output). See example below.

class ResultsHandler(webapp.RequestHandler):
    def post(self):
        k = db.Key.from_path('Engineer', the_engineer_id) #will be an integer
        e = db.get(k)
        output = {'salary': e.salary}
        output = json.dumps(output) #json encoding
        self.response.write.out(output)

Also, your url routing should look like ('/resultshandler', ResultsHandler). I don't know where /put_in_datastore came from.

Finally, notice the def post rather than def get because I'm making a POST request with the Javascript. You could do the same as a GET request, and in that case you'd use def get.

Steven Kampen
  • 631
  • 4
  • 13
  • Hmm... I keep on getting POST http://localhost:8110/resultshandler 500 (Internal Server Error). I tried doing output = {'salary':500} but no luck and alert(data) under the success function but no luck... – ejang Aug 28 '11 at 01:39
  • Don't forget the json encoding. Pastebin your code and I might be able to help. – Steven Kampen Aug 28 '11 at 01:46
  • and what error are you getting on the server? Check the logs for the python stacktrace. – Steven Kampen Aug 28 '11 at 16:59
  • 1
    ah, turns out that I assumed the python 2.6 runtime to support json.dumps. I fixed it by using simplejson.dumps function. Thanks for the dedicated 1-on-1 support :D – ejang Aug 29 '11 at 06:41
  • update, I'm getting a 'Uncaught TypeError: Cannot read 'salary' property of null'. The web worker works fine in chrome but raises this error, whereas it does not work in Firefox at all. any advice? – ejang Sep 01 '11 at 00:40