Erich Holzbauer

If you would create something, you must be something. Johann Wolfgang von Goethe (1749-1832)

Simple JSON-RPC Class

JSON-RPC is a simple methode to transfer data without site refreshing from the server to the client. It’s an alternative to AJAX calls and used in many web applications, for example most of the google apps use it for their client-server communication. Here’s my simple implementation:

The Class

jsonRPC = function() {
   cbstore = new Object();
   var counter = 0;
   var base = "insert your base url here";
   var encode = encodeURIComponent;

   var cleanup = function(id, s) {
      delete this.cbstore[id];
      setTimeout(function() {
      }, 0);
   this.send = function(params, callback) {
      var id = "cb" + ++counter;
      var timeout = setTimeout(function() {
         cleanup(id, s);
      }, 2000);
      cbstore[id] = function(data) {
         cleanup(id, s);
      var s = document.createElement("script");
      s.src = base + "?callback=cbstore." + id +
      "&" + (params);
   } = function(params, callback) {
      this.send(params, function(data) {
         if (!data || data.Status.code != "200") {
         } else {
            var c = data.pois;

The Serverside

To trigger the clientside callback function you must insert it in your server-side response. The response must look like this:

print (insert the callback function process from GET here + "({Status: { code: 200, request: "getpois" }, pois: [");

//output of your data in JSON format



mycall = new jsonRPC();"insert some params here", function(json) {
	if (!json || json == undefined || json == null)

       //do something with the response data here


What’s next

To be honest, this is a very ugly implmentation and I have to work over it quite soon. It’s now not possible to use the class for different requests. So I will add a constructor in a next version, allowing to pass a the base url when instancing the object. Also the response data object (pois) is not changeable via constructor variable -> need to be changed. (FYI: I used this class to load objects onto a visual map, therefore the name pointofinterst.) For now you can play with that code a litte bit an evaluate how it works for you as an AJAX alternative.

It's only fair to share...Share on Google+Share on LinkedInTweet about this on TwitterShare on FacebookEmail this to someonePrint this page





1 Comment

  1. A successful expert ѕhould bbe aƄle to think logically and
    analytically. Thogh seatch engines ⅼike Google claim that tɦey now ndex
    dynamic рages, аnd they do, dynamiic sites ѕtill require
    highly specialized Search Engine Optimization (SEO) treatment.
    Ꮃhether you աant to print it on business cards or display іt оn a billboard, maҝing thee domain tоо ⅼong
    or too complicated may makе it too hard for potential customers tÖ catch in onee reading Ör
    recakl ⅼater, if they ddid not have a chance tÖ writᥱ
    it dоwn.

Schreibe einen Kommentar

Powered by WordPress & Theme by Anders Norén

More in Dev
Android Tutorial

Kürzlich hatte ich das Vergnügen mich 1 Tag mit Google Android beschäftigen zu können. Dazu passend gibt es ein kleines...

High Performance MySQL bei Youtube

Paul Tuckfield,MySQL DBA bei YouTube über seine Erfahrungen mit MySQL: als Podcast oder Video