Hello,
I would appreciate some help with this…
I have a web page that is making a http request to agent, then the agent a device.send where I get a distance value. My question is how do I get that distance value which is inside the agent.on method, back to my onrequest method and then send it back to my web page?
`function requestHandler(request, response) {
dist = device.send("getDistance");
server.log(dist);
response.header("Access-Control-Allow-Origin", "*");
response.send(200,“OK”);
}
agent.on(“getDistance” {
local distance = 0;
distance = 29.1;
return distance; *** This return statement is not working
});`
`
<script>
function sendToAgent() {
var data = $("#data")[0].value;
$.ajax({
url: "https://agent.electricimp.com/qzZ.....",
data: "data="+data,
method: 'POST',
success: function(response) {
$("#response").html("Success - sent data to agent");
},
error: function (request, status, error) {
$("#response").html("ERROR - "+request.responseText);
}
});
}
</script>
<button name="data" id="data" onclick='sendToAgent()'>Get Distance</button>
<p>
<span id="response"></span>
</p>
`
Edit: Wrapped code in < code > </ code > tags.