2

I'm trying to load up the estimated world population from http://www.census.gov/ipc/www/popclockworld.html using AJAX, and so far, failing miserably.

There's a DIV with the ID "worldnumber" on that page which contains the estimated population, so that's the only text I want to grab from the page.

Here's what I've tried:

  $(document).ready(function(){
    $("#population").load('http://www.census.gov/ipc/www/popclockworld.html #worldnumber *');
  });
Josh
  • 233
  • 1
  • 4
  • 19
  • 3
    I believe there are cross-domain security policies implemented in browsers that don't allow you to access specific elements from a page originating on a different domain. I'm looking for the specifics on it, but I'm pretty sure that's the case. – user113716 Jun 09 '10 at 00:21
  • [Some more information on cross domain requests in javascript](http://stackoverflow.com/search?q=[javascript]+cross+domain+request). – Roman Jun 09 '10 at 00:22

4 Answers4

4

What you are trying to do is known as a cross-domain request. This is not a feature that browsers normally allow (security feature). Some ways to get around this limitation are described here: The jQuery Cross-Domain Ajax Guide.

Roman
  • 19,581
  • 6
  • 68
  • 84
  • Thanks, I couldn't make heads or tails of the examples on that site... However, I did find a solution which I posted below. – Josh Jun 09 '10 at 17:55
0

you can try something like this:

$.get('http://www.census.gov/ipc/www/popclockworld.html', function(content) {
    $("#population").html($('#worldnumber',$(content)));
});
Nazariy
  • 6,028
  • 5
  • 37
  • 61
  • No you can't, `get` is still attempting to do a cross-domain request, which most browsers don't allow for security reasons (with the exception of jsonp). – Roman Jun 09 '10 at 00:28
  • Just tested in Firefox. Doesn't work. Works in Safari, but I have a feeling that is just because my test page is hosted from the filesystem. I think Safari may be more forgiving in that case. – user113716 Jun 09 '10 at 00:31
0

Yeah, it's security. You can't ajax in to pages that aren't from the same domain.

stu
  • 8,461
  • 18
  • 74
  • 112
  • You **can** do [JSONP](http://en.wikipedia.org/wiki/JSON#JSONP) ( which are still ajax requests) requests to different domains. – Roman Jun 09 '10 at 00:31
0

@R0MANARMY:

I couldn't seem to follow the directions given on that site you linked to, but I did figure out a solution... I created a PHP file with the following code:

//Run cURL call
$ch = curl_init('http://www.census.gov/main/www/rss/popclocks.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);

//Set as new XML object
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);


function parseRSS($xml) {
  $cnt = count($xml->channel->item);
  for($i=0; $i<$cnt; $i++) {
    $title = $xml->channel->item[$i]->title;
    if ( preg_match("/world population estimate:\s([0-9,]+)\s/i", $title, $match) ) {
      echo $match[1];
    }
  }
}

parseRSS($doc);

Then I called it with jQuery like so:

<div id="population"></div>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    $('#population').load('getpop.php');
    var refreshId = setInterval(function() {
      $('#population').load('getpop.php');
    }, 120000);
   });
</script>

Just thought I'd post it here in case anyone else is looking to do something similar.

Josh
  • 233
  • 1
  • 4
  • 19
  • 1
    If I'm reading this right (don't really know php), you just created a server side proxy to handle the cross domain request on your behalf =). – Roman Jun 09 '10 at 18:02
  • I suppose... I just figured since AJAX has problems with accessing cross domain data, I'd just use cURL to grab the data with PHP and then access that data via jQuery/AJAX instead of trying to do it all in AJAX – Josh Jun 09 '10 at 18:06
  • 1
    One thing to be aware of (not really applicable in your case though) is that when you create a server side proxy to execute calls against other domains it has the potential to open your application up to attacks. A malicious user could, for example, use it to send information from the current page to their own server (not good if current page contains a password or whatnot). – Roman Jun 10 '10 at 13:15