
Untitled
By: a guest on
May 22nd, 2012 | syntax:
None | size: 2.36 KB | hits: 20 | expires: Never
jQuery jsonrpc 2.0 call via .ajax() gets correct response but does not work?
$(document).ready(function(){
$.ajax({
url: 'http://localhost:8080',
data: JSON.stringify ({jsonrpc:'2.0',method:'add', params:[1400,2100],id:"jsonrpc"} ), // id is needed !!
type:"POST",
dataType:"json",
success: function (result) {
alert("ok");
},
error: function (err,status,thrown) {
alert ("this syntax sucks!! " + " ERROR: " + err + " STATUS: " + status + " " + thrown );
},
complete: function (xhr,status) {
alert('Complete=> showing status as: '+ status);
data = $.parseJSON(xhr.responseText);
alert (data);
}
});
});
#! /usr/bin/python2.6
import tornado.httpserver import tornado.ioloop import tornado.web
from tornadorpc.json import JSONRPCHandler from tornadorpc import private, start_server
class MainHandler(tornado.web.RequestHandler):
def get(self,upath):
self.write( open('/home/travis/EXPLORE/webApps/index.html').read() )
class Tree(object):
def power(self, base, power, modulo=None):
return pow(base, power, modulo)
def _private(self):
# Won't be callable
return False
class Handler(JSONRPCHandler):
print ('In Handler()...')
tree = Tree()
def add(self, x, y):
print ('add() method called...')
return x+y
def ping(self, obj):
return obj
# Order is important here.. first matched handler in array is used !! handlers = [
('/RPC2',Handler),
(r"/(.*)", MainHandler),
]
start_server(handlers, port=8080)
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url: 'http://localhost:8080/RPC2',
data: JSON.stringify ({jsonrpc:'2.0',method:'add', params:[1400,2100],id:"jsonrpc"} ), // id is needed !!
type:"POST",
dataType:"json",
success: function (data) { alert("The result is : " + data.result);},
error: function (err) { alert ("Error");}
});
});
</script>
</head>
<body>
<h1> jQuery JSON RPC 2.0 demo </h1>
</body>
</html>