CppCMS
json_rpc/index.html JsonRPC Client Using XMLHttpRequest
<html>
<body>
<script type="text/javascript">
function call() {
        
        var xhr = new XMLHttpRequest();
        xhr.open("post", '/rpc');
        // Required by JSON-RPC over HTTP
        xhr.setRequestHeader("Content-Type","application/json");

        // It is better to use real formatter like JSON.js
        var x=parseInt(document.getElementById('x').value);
        var y=parseInt(document.getElementById('y').value);
        var request = '{"method":"div","params":[' + x + ',' + y +'],"id":1}';

        xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                        var res;
                        if(xhr.status === 200) {
                                // Don't call eval in real code use some parser
                                var result = eval('(' + xhr.responseText + ')');
                                if(result.error==null) {
                                        res = result.result;
                                }
                                else {
                                        res = result.error;
                                }
                        }
                        else {
                                res = 'Invalid Status ' + xhr.status;
                        }
                        document.getElementById('result').innerHTML = res;
                }
        }
        xhr.send(request); 
        return false;
}
</script>
<p><a href="usingrpc.html">Using jsonrpc.js</a></p>
<form onsubmit="return call();">
<p>
<input type="text" id="x" />
<input type="submit" value="/" />
<input type="text" id="y" /> = 
<span id="result"></span>
</p>
</form>
</body>
</html>