Cross domain scripting (or XDomain) can be good or evil, depending who is using it. But for an average Joe the Programmer XDomain scripting is blessing. Unfortunately most browsers don’t let your JavaScript to make a request to anything else than the server it was downloaded from.

Just few moments ago I had this problem when I needed to send a GET request to a RESTful server to trigger an action.
Unfortunately for me my script didn’t come from the server I needed to send the request, thus the browser killed the request on the spot.

There is a workaround, at least for simple situations like this one.
If you create an invisible iframe (style=”display: none”) you can set it’s src to the link you need to trigger and it will work without a glitch.

Here’s a sample code:

  1. <iframe id="remoteFrame" name="remoteFrame" style="display: none"> </iframe>
  2.  
  3. <a href="#" onclick="document.getElementById(’remoteFrame’).src = ‘http://restfulserver.com/get?something=1′"> Trigger the server without reloading this page!</a>
  4.  
  5. or this link: <a href="http://restfulserver.com/get?something=1" target="remoteFrame"> Trigger the server without reloading this page!</a>

I believe that with Dojo it would be even possible to have fairly easy access to the content of the iframe generated this way (I’ll check that and post the results here)

Leave a Reply