
If you were to have a subdomain that you yourself controlled / owned and you also wanted to load content from that domain via AJAX into a page how can this be done?…. We were pondering this the other day … As you know, ( or you should know) Javascript doesn’t allow cross domain XMLHttpRequest’s or AJAX requests (Asynchronous JavaScript and XML). There is a ‘dirty’ way to get around this using PHP and CURL to pull the HTML of the page you want to get the content from so JavaScript thinks it’s coming from your domain. Let me just say, this isn’t an ideal solution but it’s a useful technique when executed in the right situation.
NOTE: You need to have PHP5 installed on your server in order to use the CURL module.
The PHP
In this example we’re taking the community news section from smashingmagazine.com. Firstly using PHP we use CURL to get the whole contents of the homepage. we can then specify using javascript a specific div to get as explained below.
<?php $ch = curl_init("http://www.redbonzai.com/redbonzai/about_redbonzai.php;); $html = curl_exec($ch); echo $html; ?>
The JavaScript
This must be the simplest couple of lines of javascript ever. You can see within the DOM ready function we’re loading the content of the div #noupesoc into #content. As simple as that. You can specify any div or element on the page and grab it using this method.
< script type="text/javascript" >
$('document').ready(function() {
$('#content').load('curl.php #main');
});
</script >
The HTML
<body>
<?php //include '../includes/header.php';
$link = ' <a href="http://www.redbonzai.com/blog/?p=241">Back To Tutorial</a>';
$download = "<a href='redbonzai.com/demos/page-scrape/page-scrape.zip'<Download The Zip</a>";
?>
<h5><?php echo $link." ".$download;?></h5>
<div id="content"><img src="ajax-loader.gif" alt="Loading..." /></div>
<?php //include '../includes/footer.php';?>
</body>






