I am following the ‘Agent’ tutorial, to make a LED blink by calling an agent URL.
So far everything is working. When you open this link, my green LED will blink:
This is very cool indeed!
Of course, I don’t want my end users to open an empty HTML page, so I tried calling this URL invisibly by using jQuery.
Sadly this results in the following error:
No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
My code:
$.get( "https://agent.electricimp.com/T____XoF?green=1", function( data ) { alert( "Imp url was called." ); });
Is there a way to call agent urls invisibly, without cross-domain errors?
I’m not sure how much PHP experience you have, or if you have a normal shared webhost that allows PHP.
I usually use JQuery /AJAX with PHP handling the POST using CURL. This also fits well with JSON as both the imp and PHP make encode/decode rather easy.
There are examples … one of which is the instructable here:
// add access control allow origin so we can make AJAX requests to this endpoint
response.header("access-control-allow-origin", "*");
// check if the user sent led as a query parameter
if ("led" in request.query) {
// if they did, and led=1.. set our variable to 1
if (request.query.led == "1" || request.query.led == "0") {
// convert the led query parameter to an integer
local ledState = request.query.led.tointeger();
// send "led" message to device, and send ledState as the data
device.send("led", ledState);
}
}
// send a response back saying everything was OK.
response.send(200, "OK");
Thanks for all the tips, I was able to get my online moodlamp colorpicker working!
I now use the above mentioned PHP script so that the agent link is not visible to the end user.