If you have worked with AJAX at all, you know that there is this thing called the Same-Origin policy which makes it nearly impossible to send AJAX requests to 3rd party domains. There are workarounds such as going through a proxy. However, sometimes it’s the only way to go about getting the job done.
If you have control over both domains, then this may be the ideal solution for you. For example, lets say you have two sites and you need to share data between the sites. Connecting via MySQL could be out of the question. So what do you do now? Create a dynamic javascript include file.
Code:
<?php
//some database stuff here
echo "myVar = ['info1','info2','info3'];";
?>
See how that outputs a Javascript array? It will come in handy next.
Code:
<script src="domain2.com/dynamicscript.php" type="text/javascript"></script>
<script type="text/javascript">
for(i=1;i<myVar.length;i++)
{
document.write("<p>"+myVar[i]+"</p>");
}
</script>
By including the dynamic.php file as if it were a javascript file, you have imported the data from the other domain without having to use ajax. You can use this to pass any amount of javascript you need. I’m sure if you’re clever you will figure out how to make this into a “mock AJAX” system.
Bookmarks