Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get external html page content using jquery or ajax [duplicate]

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

like image 630
Yanker Avatar asked Dec 23 '13 12:12

Yanker


People also ask

How do you load an external webpage into a div of a HTML page?

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.

How do I display one HTML page inside another?

The HTML <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document.

Can jQuery be loaded from an external site?

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.

Which function is used to load a resource in the background in Ajax?

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.


2 Answers

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>";
like image 146
Enijar Avatar answered Sep 18 '22 20:09

Enijar


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.

like image 36
kmakarychev Avatar answered Sep 18 '22 20:09

kmakarychev