How to get html page external content using jquery or ajax

This is the question that my friend asked me today, and it hit me all day. I messed up dozens of forums looking for the right way to get external html content and show it on my page .

I want to go to http://www.someExternalURL.com and get 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); } }); 

Does not work.

Then I tried:

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

Just that the latter is only useful for local pages. And I need EXTERNAL

Any help would be greatly appreciated.

Thank you in advance

+6
source share
3 answers

There are many ways to do this using server-side code; you will get this in smaller quantities 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>"; 
+2
source

You cannot send requests to external pages in the browser if this site does not allow you. See Cross Resource Resource. But you can do this in a server application.

+2
source

You can use JSONP only if it allows the external site, performing a special implementation to return the JSON result.

You can use the url proxy hosted on your website that uses cURL, or any other means to download the right content, such as

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

+1
source

All Articles