How to update values on my web page

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.

On your webpage, you’re using PHP?

Yes.

Oh, sorry, I’m not using PHP at this point. Would it be better if I was? Here is my web page code.

`

<ul>
<br /><br /><br /><br /><br /><br /><br />
<p>
		<input type="text" name="data" id="data" value="mydata" />
		<button onclick='sendToAgent()'>Submit</button>
	</p>

</ul>
<p>
<span id="response"></span>
</p>`

Edit: Wrapped code in < code > </ code > tags.

All the html code can’t be sent over this comment window, but all what I have is a button with an onclik even that calls the sendToAgent function.

This thread shows the best round trip method.

Thank you jwehr, I’m going to take a look.