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
Asked
Active
Viewed 88 times
0
-
can you explain further? – user1647667 Mar 04 '14 at 08:27
-
means?? I just want to get the response text. – Charls Mar 04 '14 at 08:29
-
If you're talking about ajax, google about `same origin policy`. – ex3v Mar 04 '14 at 08:32
-
ya. It doesn't matter with or without ajax. if i can get the response text anyhow thats all I need thanks bro – Charls Mar 04 '14 at 08:34
-
possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Quentin Mar 04 '14 at 09:01
1 Answers
2
You need to setup a gateway
between 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