An alternative to cross-domain AJAX, sort of

Recently, I got a project to make an ad system for a website. The ad system was such that, when the page displaying ad was loaded for the first time, it would create a random link and store them in database. From then on, it would display the same link on same page.

So, I made a javascript file which would create and xmlobject for AJAX request. It would query php file on a server and get the link. But cross domain AJAX just doesn’t work. I tried to edit some parts of it, but it just wouldn’t work. Cross domain ajax requests aren’t allowed for security purposes.

So I had to think of something else. First I thought of renaming php file to js and then addind a code to .htaccess to make it behave like php. Then I thought of making image links dynamically using php’s GD library.

So, I was just surfing the net, procrastinating the job when I stumbled across:

http://www.javascriptkit.com/javatutors/externalphp.shtml

It just solved all my problems. So I just added a header for executing it as javascript and modified the echo commands to display javascript commands. Now I needed a client-side code for displaying the links. That was problematic too. I needed the source of the javascript file to be dynamic. It was just not working. But I finally found a solution by posting it on phpfreaks.com.

http://www.phpfreaks.com/forums/index.php/topic,251815.0.html

There you go.

If you want to know more details, just add a comment here.

Leave a Reply

Your email address will not be published. Required fields are marked *