6

There's this question a friend asked me today and it's bugging me all day long. I have plowwed tens of forums searching for the right way To get an external html content and show it on my page.

I want to address to http://www.someExternalURL.com and to retrieve all the html from this page. I tried the following:

$.ajax
({
    url: "http://www.someExternalURL.com",
    type: "GET",
    cache: false,
    crossDomain: true,
    data: {},
    jsonp: 'jsonCallback',
    dataType: "jsonp",
    success: function (data) {
        alert('good');
        jsonCallback = data.Result;
    },
    error: function (e) {
        alert(e.responseText);
    }
});

Didn't work.

Then I tried:

var all;
$.get("http://localhost:60939/About.aspx", function (my_var) {
    alert(my_var);
}

Only that the latter is good only for local pages. AND I NEED AN EXTERNAL

Any help'd be much appreciated.

Thanks in advance

vivek salve
  • 991
  • 1
  • 9
  • 20
Yanker
  • 292
  • 5
  • 10
  • 25
  • You can also use cURL – Jacob Dec 23 '13 at 12:19
  • Does it have to be with jQuery, because you can use 1 line of PHP to show an external webpage? – Enijar Dec 23 '13 at 12:22
  • 1
    **I wrote an answer for this question here: [Loading cross domain html page with jQuery AJAX](http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-jquery-ajax/17299796#17299796)** – jherax Jun 26 '14 at 13:52

3 Answers3

2

There are many ways to do this, using server-side code will achieve you this in less lines than JavaScript.

Using PHP you can use this:

<?
    $url = 'http://www.google.com';
    echo file_get_contents($url);
?>

Or using Perl you can use:

#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->get("http://www.google.com");
my $content = $mech->res()->content();

print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>Perl HTML Parsing</title>";
print "</head><body>";
print $content;
print "</body></html>";
Enijar
  • 6,387
  • 9
  • 44
  • 73
  • I don't know about C# or Python but I'm sure a quick Google search for them will yield the results you are looking for. – Enijar Dec 23 '13 at 12:48
2

You can't make requests to external pages in browser if that site don't allow you this. See Cross Origin Resource Sharing But you can do this in server application.

kmakarychev
  • 731
  • 4
  • 14
1

You can use JSONP only if the external site allows it, by doing a special implementation on how the JSON result is returned.

You can use a url proxy hosted on your website that uses cURL, or whatever means to download the desired content such as

http://YOURSITE.com/get.php?=http://www.EXTERNALSITE.com/json

Rami Sakr
  • 372
  • 1
  • 14