Monday, October 12, 2015

The Hello World of Google Protocol RPC (javascript part)

The Hello World of Google Protocol RPC using python is here https://cloud.google.com/appengine/docs/python/tools/protorpc/#The_Hello_World_of_ProtoRPC

with an example on Testing the Service from the Command Line

Here I add the javascript part on how to test the RPC service in the browser using the google app engine
templates/index.html    Select all
<!DOCTYPE html> <html><head><title>post service demo</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script language="javascript" type="text/javascript"> function submitform() { $.ajax({ headers: { "Accept": "application/json", "Content-Type": "application/json" }, type: "POST", url : "/hello.hello", dataType : "json", data : JSON.stringify({"my_name": $("#my_name").val()}), error: function () { alert("loading Ajax failure"); }, onFailure: function () { alert("Ajax Failure"); }, statusCode: { 404: function() { alert("missing info"); } }, success: function (response) { alert("The server says: " + JSON.stringify(response)); } }) .done(function(data) { $("#result").text(data["hello"]); }); };</script></head> <body> <h1>Hello world!</h1><br> <p><form id="myform" method="post" action="/hello.hello"><input type="text" id="my_name" name="my_name" value="javacom"><input type="button" id="submit" value="submit" onclick="submitform();"></form></p> <p><div id="result"></div></p> </body> </html>


main.py    Select all
import os import jinja2 import webapp2 TEMPLATE_DIR = 'templates' JINJA_ENVIRONMENT = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), TEMPLATE_DIR)), extensions=['jinja2.ext.autoescape'], autoescape=True) class MainHandler(webapp2.RequestHandler): def get(self): template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render()) app = webapp2.WSGIApplication([ ('/', MainHandler) ], debug=True)


app.yaml    Select all
application: helloworld version: 1 runtime: python27 api_version: 1 threadsafe: yes handlers: - url: /hello.* script: services.app - url: .* script: main.app libraries: - name: webapp2 version: "2.5.2" - name: jinja2 version: "2.6"



No comments: