• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

JSON-RPC need help

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 2.1k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    tbe
    last edited by Aug 8, 2017, 5:55 AM

    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

    J 1 Reply Last reply Aug 8, 2017, 1:26 PM Reply Quote 0
    • J Offline
      j.e.f.f Project Sponsor Module Developer @tbe
      last edited by Aug 8, 2017, 1:26 PM

      @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

      1 Reply Last reply Reply Quote 1
      • T Offline
        tbe
        last edited by Aug 9, 2017, 7:36 PM

        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.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        1 / 1
        • First post
          1/3
          Last post
        Enjoying MagicMirror? Please consider a donation!
        MagicMirror created by Michael Teeuw.
        Forum managed by Sam, technical setup by Karsten.
        This forum is using NodeBB as its core | Contributors
        Contact | Privacy Policy