0

I want to get the response text from google.com and display it. (using alert or whatever). I know there are problems with domains and others. anyone have any idea of getting it. I searched a lot but still stuck in there :( please help

Charls
  • 235
  • 1
  • 4
  • 13

1 Answers1

2

You need to setup a gatewaybetween the remote site and your script.

For example, create a simple PHP function where you fetch the desired address and return the ouput:

<?php

function getRemoteContent($address) {
 header("Content-Type: text/html");

 $content = file_get_contents($address);
 return $content;
}

print getRemoteContent($_GET['url']);
?>

After, you need to link your Ajax action to this function (for example, if your file is localised in a file called remote.php

$.ajax({
 url: 'remote.php',
 data: {url: 'http://www.google.fr'},
 success: function(data) {
   alert (data);
 },
 error: function() {
   alert ('oups');
 }
});

Note: untested and very simple example ;)

Note I suggest the read of this tutorial for further explanation

Paul Rad
  • 4,820
  • 1
  • 22
  • 23
  • `getRemoteContent` performs a query from the server side (you need this PHP "gaterway") to fetch the remote website content. You can't access to remote domains (which not allowing explicitly your domain queries) directly from Javascript. PS: sry for my english – Paul Rad Mar 04 '14 at 08:56
  • thanks bro. but something wrong. what is the use of print getRemoteContent??(i removed it and code got ok) and in $ajax, what are 2 urls. 1st url for our home page ryt? and when i inserted it the alert displays the source of the given page not the page given by data: {url: 'http://www.google.fr'} then i set that url to google.com but then alert('oups') displayed dnt knw whats going on there – Charls Mar 04 '14 at 09:02