Ajax Content From Other Domains
Ajax has a built-in restriction allowing it to retrieve
content only from its own domain. In other words, the domain
where the web page is loaded into the browser is the only
domain Ajax can retrieve content from.
This article shows you how to get around that restriction.
Ajax asks PHP or CGI or other software on the server to go
get the content on another domain. That software gets the
content and hands it to Ajax. Both a PHP version and a CGI
version of the server software are provided below.
Getting Started Instructions
First, install one software file on your server. Then, use
the Ajax engine.
These instructions assume you already have a web page with
the Ajax engine from the
Copy and Paste Ajax Engine
article. If you don't have it already, go get it. The Ajax
engine is really and truly copy 'n paste.
Determine which type of software you're more comfortable
with, PHP or Perl CGI. Copy the preferred software and use
a plain text processor to save it on your computer. The
example further below assumes the PHP file will be named
getpage.php. File names can be whatever is appropriate. If
you decide on the Perl CGI one, perhaps it can be named
getpage.cgi
Here is the PHP version of the software to get content from
another domain.
Here is the CGI version of the software to get content from
another domain.
Whichever script you decided to use, one place needs to be
customized. The script will need to be told which domain or
domains it is allowed to retrieve content from. Web site IP
addresses may also be listed.
Both the PHP and the CGI scripts have the place marked where
you specify the domain names.
The reason specifying allowed domains is required is to
prevent cross-site scripting from getting content from just
any domain and sending it to your site visitor's browser.
Whichever domains you allow the script to retrieve content
from can, at any time, send content you might not expect and
it could even be dangerous. It is suggested you either
control the domains you list or trust them without
reservations.
After the script has been updated with the allowed domains,
it is ready to install.
To install the PHP version, upload the script to your server
where your browser can access it. In the same directory
where you have your domain's main or index page should work
fine.
To install the CGI version, Perl modules LWP::UserAgent and
HTTP::Request::Common need to be available on your server.
Most hosting companies provide those modules. If in doubt,
ask your hosting company or use
Master Pre-Installation Tester
to find out.
To install, upload the CGI script to your server in a
directory allowed to run Perl CGI software. Upload with an
FTP program as a plain text file (not as binary), then give
the uploaded file 755 permissions.
To test, first determine the URL of a web page to retrieve
the content of. The web page must be on a domain that the
script is allowed to retrieve content from.
Now, into your browser's address bar, type the URL of the
script, followed by a question mark ("?"), followed by the
URL of the web page.
The script should retrieve the web page at the URL and
display it in the browser.
Instructions for Using Ajax to Retrieve Content From Other Domains
You'll need three things:
-
The web page with the Ajax engine.
-
The URL of the script you just installed.
-
The URL of a web page on one of the allowed domains.
On the web page with the Ajax engine, you should already
have a div ready to contain the content that will be
retrieved. If unsure, see the
Copy and Paste Ajax Engine
article for instructions.
We'll assume the div's id value is "MyUniqueID" like the
example in the above-mentioned article.
Let's further assume the script you just uploaded is at
http://(YourDomain)/getpage.php and that the web page you
will be retrieving is at http://example.com/apage.html
Here is a way to retrieve content from that other domain
with Ajax and insert it into the web page currently in the
browser.
That's all there's to it. Simply click the link and the
content at http://example.com/apage.html will be inserted
into your web page.
Uses for Ajax with Content from Other Domains
The example of retrieving a web page from another domain and
inserting it into the current web page was just an example.
In a real implementation, Ajax is likely to retrieve content
generated by other scripts.
And that is where the power of Ajax and it's elegant ease of
use can be appreciated.
As a possibly obvious example, a feedback form on one
domain can be used on all of your domains. The form can be
loaded immediately after the web page, completely bypassing
any delay the form load might otherwise have required. If
the form needs to be changed, one change changes it
everywhere. (If you've had to fight form spam, you will
appreciate that.)
The latest bid, the current weather, the time, the most
recent forum or blog post, specials from other domains,
widgets, all are examples of content that can be inserted
into web pages after the pages have been loaded into the
site visitor's browser.
Consider that the output of software on any of your domains
can be inserted into web pages on your other domains. That's
the elegance and sheer pleasure of using Ajax this way.
Will Bontrager
©Copyright 2008 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.