Sample Javascript API usage

From RainMachine wiki page
Jump to: navigation, search

Sample Javascript API usage

<html>
<head>
    <script type="text/javascript">
        var host = "192.168.12.157"; 
        var port = "8080";
        var apiUrl = "https://" + host + ":" + port + "/api/4";
        function getApiVer() {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", apiUrl + "/apiVer", false);
            xmlhttp.send();
            alert(xmlhttp.responseText); 
        }
        function postAuthLogin() {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("POST", apiUrl + "/auth/login", false);
            xmlhttp.setRequestHeader("Content-type","text/plain");
            xmlhttp.send(JSON.stringify({
                pwd: document.getElementById("password").value,
                remember: document.getElementById("remember").checked ? 1 : 0
            }));
            alert(xmlhttp.responseText);
        }
    </script>
</head>
<body>
    <h1>Sprinkler Mini API Example</h1>
    <ul>
        <li><a href='apiVer' onclick="getApiVer(); return false;">GET <b>apiVer</b></a></li>
        <li><a href='auth/login' onclick="postAuthLogin(); return false;">POST <b>auth/login</b></a>
             
            Password: <input type="text" id="password" value=""/>
             
            Remember: <input type="checkbox" id="remember" checked/>
        </li>
</body>
</html>

Other API examples can be found here: https://github.com/sprinkler/rainmachine-developer-resources/tree/master/api-examples