Calling our actuator API from JavaScript

Calling the actuator API is done slightly differently, since we use the POST method instead of the GET method. We need to send some content with the request. Sending text-based content is easy. We only need to send a string in the send() method. But we still need to set the Content-Type header to describe what type of text-based content we want to send. In our example, we use plain text:

var Span = document.getElementById("OutputState"); 
var CurrentState = Span.innerHTML; 
var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function () 
{ 
   if (xhttp.readyState == 4) 
   { 
          if (xhttp.status == 200) 
          { 
                 var Data = JSON.parse(xhttp.responseText); 
                 Span.innerHTML = Data.output ? "ON" : "OFF"; 
          } 
 
          delete xhttp; 
   } 
}; 
 
xhttp.open("POST", "/Set", true); 
xhttp.setRequestHeader("Accept", "application/json"); 
xhttp.setRequestHeader("Content-Type", "text/plain"); 
if (CurrentState == "ON") 
   xhttp.send("OFF"); 
else 
   xhttp.send("ON"); 

The following image shows what our very simple user interface might look like in a browser:

Actuator Markdown document, converted to HTML
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.135.224