Created
October 19, 2011 13:30
-
-
Save larscwallin/1298281 to your computer and use it in GitHub Desktop.
SIMPLX RPC - Super simple JavaScript implementation of JsonRPC.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"> </script> | |
<script type="text/javascript"> | |
var simplx = new Object(); | |
simplx.jsonrpc = (function(){ | |
var response = null; | |
var self = this; | |
this.url=""; | |
this.host=""; | |
this.path=""; | |
this.pageid=""; | |
this.parametername=""; | |
this.rpc={ | |
jsonrpc:"2.0", | |
id:"1", | |
method:"", | |
params:{} | |
}; | |
this.callback=null; | |
this.errorcallback=null; | |
this.init = function(options){ | |
self.host = options.host || self.url; | |
self.service = options.service || self.pageid; | |
self.parametername = options.parametername || ""; | |
self.rpc= options.rpc || self.rpc; | |
self.callback=options.callback || self.callback; | |
self.errorcallback=options.errorcallback || self.errorcallback; | |
}; | |
this.execute = function(){ | |
var data = ""; | |
var completeUrl = (self.host + "" + self.service); | |
var rpc = JSON.stringify(self.rpc); | |
response = $.ajax({ | |
type:"POST", | |
url:completeUrl, | |
data:rpc, | |
contentType:"application/json-rpc", | |
success:function(data){ | |
if(data){ | |
try{ | |
data = JSON.parse(data); | |
}catch(e){ | |
// The data might just have been cast to Array already as the response type is application/json | |
} | |
if(!data.error){ | |
self.callback(data); | |
return true; | |
}else{ | |
if(self.errorcallback){ | |
self.errorcallback(data); | |
} | |
return false; | |
} | |
} | |
} | |
}); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment