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
To load external HTML into a <div>, wrap your code inside the load() function. To load a page in div in jQuery, use the load() method.
The HTML <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document.
Is it possible to load a single page from an external website? Yes, it's possible, but you'll need 1 line of PHP :) If you only need RSS feeds and you don't mind relying on Google you could use jquery-feeds.
The browser performs a JavaScript call to the Ajax engine. In other words, create an XMLHttpRequest object. In the background, an HTTP request is made to the server and the appropriate data is retrieved. HTML, XML, or JavaScript data is returned to the Ajax engine which then delivers the requested data to the browser.
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>";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With