AJAX and the Dynamic Script Tag
Monday, 29 May 2006
I didn’t know that javascript can be made to execute requests to sites that didn’t originate from the original server. Jason Levitt details this technique at XML.com:
if you make web services requests using the dynamic script tag approach — and the web service lets you specify a JavaScript callback function — you can have unfettered access to the web service in a seamless, cross-domain, cross-browser fashion.
Confused? Here are the steps:
1. The server is designed to respond to the following request.
http://www.test.com/test.php?functionname=myfunction¶m1=aaa¶m2=bbb
With the following:
myfunction({'Value1': 'foo1', 'Value2': 'foo2'})
2. Next you take the results and write it into a script tag to execute it.
<script>
function myfunction(params)
{
alert (params['Value1']);
}
</script>
<script>
myfunction(({'Value1': 'foo1', 'Value2': 'foo2'});
</script>
Via Stuart Holoway