JSON-RPC need help



  • i´m a beginner with javascipt and need help . how can i call the json-rpc function ( getData ) to get
    data from API and how can i parse it, to display the result an MM.
    the result of the call should be an JSON-RPC string .

    	getData : function(){
    	
    		var self = this;
    		var json = "http://xxx.xxx.xxx.xxx:3777/api/";		
    		var retry = true;
    
    
    	      var HTTP = new XMLHttpRequest();
    		    HTTP.open("POST",json,true);
    		    var rpc = JSON.stringify({"jsonrpc":"2.0", "method":"GetValue", "params":[18697], "id":0});
    		   HTTP.setRequestHeader("Content-type", "application/json");
    		   HTTP.send(rpc);
    		 
    	 },
    
    

    Thanks Thomas


  • Module Developer

    @tbe You need to add an event listener to react to the response. example:

    HTTP.onreadystatechange = function() {
      if (HTTP.readyState == 4 && HTTP.status == 200) { //response complete and OK
        //do something with HTTP.response;
      } else if (HTTP.readyState == 4) { //response complete but some error
        console.log("Error: " + HTTP.status + " " + HTTP.statusText);
      }
    }
    

    Alternatively you can add individual event listeners for different states:

    HTTP.addEventListener("progress", updateProgress); 
    HTTP.addEventListener("load", transferComplete); 
    HTTP.addEventListener("error", transferFailed);
    HTTP.addEventListener("abort", transferCanceled);
    

    Full documentation on the XMLHttpRequest object here:
    https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest



  • Thanks. But i have an other problem.
    The JSON request works per curl on console, but in js : "net::ERR_EMPTY_RESPONSE "

    Curl : 
    curl -i -X POST -H "Content-Type: application/json" -d "{\"jsonrpc\": \"2.0\", \"id\": \"0\", \"method\": \"GetValue\", \"params\": [18697]}" http://xxx.xxx.x.xx:3777/api/
    

    but in the MM-JS-Script don´t work. Looks like the json parameter don´t work.


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.