diff --git a/angular-json-rpc.js b/angular-json-rpc.js index 49eb30e..0c42c2c 100644 --- a/angular-json-rpc.js +++ b/angular-json-rpc.js @@ -4,64 +4,59 @@ angular.module('angular-json-rpc', []); // spec - http://www.jsonrpc.org/specification // var jsonRpc = angular.module('angular-json-rpc', []).factory("jsonRpc", ['$http', function($http) { - this.version = "2.0"; +this.version = "2.0"; + this.url = null; - this.url = null; + // setup rpc + this.setup = function (params) { + // check params + check_params(params); + this.url = params.url; + return this; + } - // - // setup rpc - // - this.setup = function(params){ - // check params - check_params(params); - this.url = params.url; - return this; - } + success = function (data, status, headers, config) { - success = function(data, status, headers, config){ + } - } + error = function (data, status, headers, config) { - error = function(data, status, headers, config){ + } - } - - // - // json-rpc request - // - this.request = function(method, options){ - if(options === undefined) - options = { id: 1 }; + // json-rpc request + this.request = function (method, options, successCallback, errorCallback) { + if (options === undefined) + options = { id: 1 }; if (options.id === undefined) - options.id = 1; + options.id = 1; // make request - var bodyRequest = JSON.stringify({"jsonrpc": this.version, "method": method, "params": options.params, "id" : options.id}); - var headers = {'Content-Type': 'application/json'}; - $http({'url': this.url, 'method': 'POST', 'data' : bodyRequest, 'headers': headers}) - .success(function (data, status, headers, config){ - return data; + var bodyRequest = JSON.stringify({ "jsonrpc": this.version, "method": method, "params": options.params, "id": options.id }); + var headers = { 'Content-Type': 'application/json' }; + $http({ 'url': this.url, 'method': 'POST', 'data': bodyRequest, 'headers': headers }) + .success(function (data, status, headers, config) { + successCallback(data); }).error(function (data, status, headers, config) { - return data; + errorCallback(data); }); // return return true; - } + } - // - // Check params - // - check_params = function(params){ - if (params == undefined){ - throw("Wrong params"); - } + // + // Check params + // + check_params = function (params) { + if (params == undefined) { + throw ("Wrong params"); + } - if (typeof(params.url) !== 'string' || params.url == '') - throw("Wrong url parameter"); - } + if (typeof (params.url) !== 'string' || params.url == '') + throw ("Wrong url parameter"); + } - return this; + return this; -}]); \ No newline at end of file +}]);