3

How do I get JSONRPC working properly with JQuery on the client-side, on the referenced types of calls?

To represent my attempt, I've tried to create the smallest possible test-case:

Server[1]

from txjsonrpc.web import jsonrpc
from twisted.web import server
from twisted.internet import reactor

class Math(jsonrpc.JSONRPC):
    def jsonrpc_add(self, a, b):
        return a+b

    def jsonrpc_echo(self, foo):
        return str(foo)

    def jsonrpc_hello(self):
        return u"Hello World!"

reactor.listenTCP(7080, server.Site(Math()))
reactor.run()

Client[2]

<!DOCTYPE HTML>
<html>
    <head>
        <title>Math is fun?!</title>
        <script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="https://raw.github.com/datagraph/jquery-jsonrpc/master/jquery.jsonrpc.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.jsonRPC.setup({
                    endPoint : 'http://localhost:7080',
                    namespace : ''
                });
                $("input#evaluate").click(function() {
                    $.jsonRPC.request('jsonrpc_echo', {
                        params : [$("textarea#input").val()],
                        success : function(data) {
                            $("<p />").text(data.result).appendTo($("p#result"));
                        },
                        error : function(data) {
                            $("<p />").text(data.error.message).appendTo($("p#result"));
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <p id="result"></p>
        <p>
            <textarea name="input" id="input"></textarea>
        </p>
        <p>
            <input name="evaluate" id="evaluate" value="evaluate" type="button" />
        </p>
    </body>
</html>
  1. Adapted from https://stackoverflow.com/a/4738563/1438003
  2. Adapted from https://github.com/filonenko-mikhail/maxima-json-rpc/blob/master/examples/js-maxima-rpc-client.html
Community
  • 1
  • 1
user1438003
  • 6,603
  • 8
  • 30
  • 36
  • I'm neither getting expected output nor errors :/ – user1438003 Jul 22 '12 at 11:56
  • There are several JSON-RPC plugins for jQuery. Which one are you using? – Pointy Jul 22 '12 at 12:01
  • [JQuery-JSONRPC](https://github.com/datagraph/jquery-jsonrpc/), though admittedly it should be possible using just `stringify`, i.e.: without a plugin atop JQuery – user1438003 Jul 22 '12 at 12:04
  • Well can you tell (via Firebug or TamperData or some other debug tool) whether the HTTP request is being made? Are there logs on the server? – Pointy Jul 22 '12 at 12:08
  • Hmm: "XMLHttpRequest cannot load http://localhost:7080/?tm=1342959087286. Origin null is not allowed by Access-Control-Allow-Origin." - Is this where I use JSONP?! – user1438003 Jul 22 '12 at 12:13
  • If you're loading your page as a "file://" URL, then a request to that "localhost" URL is cross-domain. If you load your page from that same server, however, it should work. – Pointy Jul 22 '12 at 12:15
  • Okay, I'm running it from localhost now, and all it's giving me is a "Load cancelled" error from JQuery. Also, in production I'll need to load it from file, since it'll be packaged into a PhoneGap app. – user1438003 Jul 22 '12 at 12:18
  • Well PhoneGap doesn't have cross-domain restrictions. I don't know what "Load cancelled" means. – Pointy Jul 22 '12 at 12:19
  • I'm only getting the load cancelled error in Chrome, on the PhoneGap app I can't get any debug messages from the JS, it just isn't loading anything. :/ – user1438003 Jul 22 '12 at 12:22

0 Answers0