JQuery jsonrpc 2.0 call via .ajax () gets the correct answer, but not working?

A jsonrpc 2.0 call through jquery to the Tornado web server receives a “200 OK” http response and my network sniffer shows the decoded response as containing

{"jsonrpc": "2.0", "error": null, "result": 3500, "id": "jsonrpc"}

ie valid jsonrpc 2.0 answer. 3500 is also the correct result, RPC was a simple add function.

However, firebug does not display the response and .ajax success . does not start. The answers are .ajax () error and complete . but gave me no idea about the problem. Here's index.html, which calls the ajax () call.

$(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); } }); }); 
+4
source share
1 answer

I realized that the problem was to open index.html with Firefox "File Open" instead of my web server delivering index.html to me (by looking at http: // localhost: 8080 )

Here is a complete working example of making a JSON RPC call and displaying the result with a simple warning. RPC is the main add function.

To see it in action:

  • Save index.html (2) and webserver.py (1). Change webserver.py to reflect the location of index.html

  • Run webserver.py (chmod a + x webserver.py. Sudo./webserver.py)

  • Launch Firefox and go to localhost: 8080. This will load index.html, call ajax (), and display the result with a warning.

(1) The Tornado web server uses the tornadorpc module and is written in Python. Here he is:

 #! /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) 

(2) index.html uses the jquery ajax () method to call JSONRPC to add a remote procedure. Make sure you save it according to the way the web server in (1) is trying to read its contents.

 <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> 
+5
source

Source: https://habr.com/ru/post/1381426/


All Articles