Python JSON RPC with jQuery - ServiceRequestNotTranslatable

I am writing a web application using the python JSON-RPC implementation - http://json-rpc.org/wiki/python-json-rpc on the server side and jQuery axaj API on the client side. This is my first implementation of the JSON service in python, so I copied the example from the mentioned site (CGI runs on Apache 2.2):

#!/usr/bin/env python

from jsonrpc import handleCGI, ServiceMethod

@ServiceMethod
def echo(msg):
    return msg


if __name__ == "__main__":
    handleCGI()

Everything works fine with the supplied Python ServiceProxy class as a client (in the console):

from jsonrpc import ServiceProxy
s = ServiceProxy("http://localhost:8080/mypage/bin/controller.py")
print s.echo("hello")

But when I try to make an ajax call using jQuery in the firebug console (in the context of my page):

var jqxhr = $.getJSON("bin/controller.py", {"params": ["hello"], "method": "echo", "id": 1}, function(data) { alert('success!'); });

I keep getting this error:

{"error":{"message":"","name":"ServiceRequestNotTranslatable"},"result":null,"id":""}

What am I doing wrong?

+3
source share
2 answers

Here's how to make a JCON RPC call in jQuery:

$.ajax({url: "bin/controller.py",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify({"jsonrpc": "2.0",
        "method": "echo", "params": ["hello",], "id": 1,
    }),
    dataType: "json",
    success: function(response) {
        alert(response.result);
    },
});

HTTP- POST, .

​​ JSON. , jQuery.ajax URL- , (.. "Method = echo & params =..." ). , JSON.stringify contentType "application/json", , JSON "application/x-form-urlencoded".

dataType: "json" jQuery (, , JSON), .

+4

, flask, jquery.

from flask import Flask, jsonify, render_template, request
app = Flask(__name__)

@app.route('/echo')
def echo():
    return jsonify({'result': request.args.get('params')})

@app.route('/')
def index():
    return """<!doctype html><head>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
       <script type="text/javascript">
         $.get('/echo?params=hello', function(data) {
           alert(data['result']);
         });
       </script>
       </head></html>"""

if __name__ == '__main__':
    app.run()
+3

All Articles