Tolu
Tolu

Reputation: 43

How can I return a value from google app engine to jquery?

After a user enters data on my home page, I wish to send the values to a google app engine python script for processing. When the script is done processing, I want it to pass the values back to my home page which will display the results on the lower part of the page. Since I don't want to reload the whole page, I want to use jquery when user hits submit.

I have 2 questions 1) How do I pass the results back to my home page from within python 2) In my $.ajax call, how do I specify the name of the python script which will handle the processing

jquery code
$(function() {  
  $("input#Submit").click(function() {  
    $.ajax({  
  type: "POST",  
  url: "/",  //my python script
  data: {
       var1: $("input#var1").val(),
       var2: $("input#var2").val()      
  },
  success: function(returnData){
    alert(returnData)
  }

});  
});

python script
class processInfo(webapp.RequestHandler):
    var1 = self.request.get("var1")
    var2 = self.request.get("var2")
    do some processing here e.g.
    var 3 = var1* var2
    how do i return var 3 back to the $.ajax call?

application = webapp.WSGIApplication( ('/', processInfo), debug = true)

Upvotes: 4

Views: 238

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

The returnData argument to the success function in your Javascript will be passed the body of the response to the HTTP request. Simply write the data you want with self.response.out.write in your handler.

Specifying what script to call is done the same as anywhere else: Map the handler in your WSGI app (in your sample, it's mapped as '/', which you probably don't want), and ensure the handler script is mapped correctly in app.yaml. Handling an AJAX call is no different to handling any other standard HTTP request.

Upvotes: 4

Related Questions