12

Is it possible to load a single page from an external website?

I am trying to show up a single page but cannot seem to get it to work

$("#response").load("http://domain.com", function(response, status, xhr) {
   if (status == "error") {
      var msg = "Sorry but there was an error: ";
      alert(msg + xhr.status + " " + xhr.statusText);
   }
 });

Help would be greatly appreciated

ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • 1
    Yes, it's possible, but you'll need 1 line of PHP :) – Roko C. Buljan Feb 21 '13 at 10:12
  • Take a look here - http://en.wikipedia.org/wiki/Cross-origin_resource_sharing and here http://msdn.microsoft.com/en-us/library/windows/apps/hh767443.aspx – Bakudan Feb 21 '13 at 10:17
  • If you only need RSS feeds and you don't mind relying on Google you could use [jquery-feeds](https://github.com/camagu/jquery-feeds/). – the Oct 25 '14 at 20:10

2 Answers2

23

You're running into a cross domain policy issue cause AJAX (for security reasons) will not let you grab content from a page that does not sit on the same domain.

To get rid of it and accomplish your task:
you need a PHP file you can call grabber.php with just this line of PHP:

<?php echo file_get_contents($_GET['url']); ?>

Than inside your html (or whatever file just do like:)

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>test</title>
</head>
<body>

    <div id="response"></div>

</body>

<script>
$(function(){
    var contentURI= 'http://domain.com #element';    // URL TO GRAB + # of any desired element // if needed :)
    $('#response').load('grabber.php?url='+ contentURI);
});
</script>

</html>

Why does this work?

  • now, AJAX is sending a simple GET request to the grabber.php page,
  • grabber.php echoes the desired content
  • now the content is on your (server) domain!
  • and AJAX is happy to serve you :)
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • 1
    this is great. One issue though. I notice many sites lose their absolute paths once pulling it. Do you know any tips to pull the page and keep it intact? – barrylachapelle Mar 05 '14 at 04:14
  • 1
    @andehlu You could add the loaded contents into an iframe, this should keep the paths in order. – Wessam El Mahdy Jul 24 '16 at 17:44
  • @WessamElMahdy depents if you just want to admire the loaded page or you want to actually do some stuff with the loaded elements.... – Roko C. Buljan Jul 24 '16 at 19:21
  • The comments speak to my problem, which is that the page content that loads is a frameset, and the frameset contents lose their absolute paths, resulting in "Page Not Found" or "Object Not Found" errors as the only content. – TARKUS Aug 31 '16 at 13:47
  • keep in mind that with this soution the second url's server sees the ip of your server not the client's ip. – DrLightman Dec 20 '16 at 16:40
1

Are you trying to load a page on a different domain?

If yes, then it seems you got a cross-domain policy on your way...

andri
  • 1,021
  • 1
  • 9
  • 16