Hello everyon,
 
I try to communicate with a server in Python using JRPC . But I encounter the following error : [ HTTP / 1.1 501 Unsupported method ( ‘OPTIONS’ ) 3ms ].
 
Below is my code :

<!DOCTYPE html>
<html>
    <head>
        <title>JQuery & pyjsonrpc demo</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript"
                src="js/zebra.js">
        </script>
        <script type="text/javascript">

            function fnSum(form) {
                var url = "http://localhost:4000/jsonrpc";
a = parseInt(form.a.value);
b = parseInt(form.b.value);

                //var request = {};
                //request.method = "add";
                //request.params = [a, b];
                //request.id = 1; // RPC server echo this id again.
                //request.jsonrpc = "2.0";
                //form.s.value = "";

//----------------------------------
// My code works with JQUERY but not with Zebra
                //$.post(url, JSON.stringify(request), displaySearchResult, "json");

                service = new zebra.io.JRPC(url, ["add"]);
                service.add(a, b, function (response) {
                    if (response.result)
                        form.s.value = response.result;
                    else if (response.error)
                        alert("error: " + response.error.message);
                    else
                        alert("Unknown response");
                });

//----------------------------------
            }
        </script>

    </head>
    <body>
        <h1>JQuery & pyjsonrpc demo</h1>
        <form>
            <input type="text" name="a" value="2"/> +
            <input type="text" name="b" value="6"/> =
            <input type="text" name="s"/>
            <input type="button" onclick="fnSum(this.form)" value="Sum"/>
        </form>
    </body>
</html>

The code source of my python server

Start your code here
import pyjsonrpc

def add(a, b):
    """Test function"""
    return a + b

def echo(param):
    print "from client:", param
    return param

class RequestHandler(pyjsonrpc.HttpRequestHandler):
    # Register public JSON-RPC methods
    methods = {
        "add": add,
        "echo": echo,
    }

    def end_headers(self):
        """
        Override override this method to add a response header
        for "Access-Control-Allow-Origin: *".
        Otherwise web browser complaints 'Origin is not allowed by Access-Control-Allow-Origin'
        error.
        """
        self.send_header("Access-Control-Allow-Origin", "*")
        super(RequestHandler, self).end_headers()

addr = ('localhost', 4000)
# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
    server_address = addr,
    RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "Listening: ", addr
http_server.serve_forever()

 

Please login or Register to Submit Answer