http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
here is a same code.
replace “ajax_info.txt” with your request example “/digitalWrite?pin=0&state=HIGH”
example here
<html>
<body>
<script>
function setHigh(pin, cb) {
setPinValue(pin, "HIGH", cb)
}
function setLow(pin, cb) {
setPinValue(pin, "LOW", cb)
}
function setPinValue(pin, val, cb) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
cb(xhttp.responseText);
}
};
xhttp.open("GET", "/digitalWrite?pin="+pin+"&state="+val, true);
xhttp.send();
}
// use it like this
setHigh(5, function(resp) {
console.log(resp);
});
</script>
</body>
</html>
1 Like