View Full Version : Cross-domain AJAX tutorial anyone??

08-26-2006, 03:15 PM
Hi all,
I was just curious about cross-domain AJAX and I wanted to know how it works ... so if anyone knows how it works or has a tutorial about it please kindly post it here, it may not only help me but also some others who are intrested in this subject. Thanks!!

08-26-2006, 03:23 PM
It is not possible. You can use PHP to get data from another domain, but not with just javascript.

08-26-2006, 03:58 PM
blm126: I *am* using PHP. I don't think that making interactive pages(which do dynamic queries) can be done only with javascript. I hope that it makes sense.:)

08-26-2006, 04:50 PM
I was just curious about cross-domain AJAX and I wanted to know how it worksA server-side script on your domain is used to load and output the content, then the XMLHttpRequest object reads that file like normal. A simple example (not that you should use this, of course; much more checking is required) is:
// golden.php
?>By open()ing:
xhr.open("GET", "golden.php?url=" + encodeURIComponent("http://www.google.com/"), true);that file with the correct GET variable, any page on the Internet can be retrieved.

08-26-2006, 05:01 PM
Twey: even google search results?? and json objects(or whatever)?

08-26-2006, 05:14 PM

08-26-2006, 05:15 PM
Oh cool!!! Can you give me some more examples of data fetching Twey??

08-26-2006, 05:23 PM
Er? What more is there to demonstrate? :-\

08-26-2006, 05:31 PM
I mean can I just bring whole web pages with it and XML data islands??

08-26-2006, 05:36 PM
Of course. Just like you can with normal AJAX. It's just redirected through a server-side script, is all.

08-26-2006, 07:40 PM
Oh cool thing. The only problem I am having is extracting images. Any ideas about that?? I am using the same code you included by the way.

That may be because of incomplete image path(images/someimage.png), isn't it?? If so is there any fix for that??

08-26-2006, 07:47 PM
That may be because of incomplete image path(images/someimage.png), isn't it??Yes.
If so is there any fix for that??Probably using PHP, you must go through all the URLs and convert them to absolute ones.

08-27-2006, 06:14 AM
Well, how do I do that then?? Use str_replace or preg_replace??

08-27-2006, 04:21 PM
You'd probably be able to do it with some complex regex, yes. I'm not all that good with regex though.
You'd have to go through each instance of (src|href)="[^"]+", check if it were an absolute URL, and if not modify it by prepending its host if it began with a / or else its original directory.

08-27-2006, 05:19 PM
I am not at all good at regexps too.:(

08-28-2006, 06:11 PM
For cross domain Ajax...


Ajax => Request to self domain (.php file with curl code) => call php file of remote server [also allow get/head/post methods... as we are using in payment gateway connection like authorize.net-Advanced technique.]

Now Curl is throwing back output to Ajax in responseXML or responseText.