How to call agent link without actually opening the page in the browser?

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:

https://agent.electricimp.com/T____XoF?green=1

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?

@ElectricEerk, you may want to edit your post to remove the agent URL if (a) it’s genuine and (b) you don’t want the rest of us to access it!

You can use PHP CURL method.

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:

See steps 5 and 6

Is there a way to call agent urls invisibly, without cross-domain errors?

This is because of security rules in browsers. If you add an “Access-Control-Allow-Origin” header to the response, you can call it from your webpage:

`function requestHandler(request, response) {
try {

// 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");

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

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.