Leila Hamon
Leila Hamon

Reputation: 2595

Is it possible to make a successful ajax get request to google.com from any page that has jquery loaded?

I want to fetch google.com successfully, I don't care if I use jquery, I just want to fetch it somehow (i.e. not using a javascript library is fine, e.g. XMLHttpRequest is fine too). Here's an example ajax call I've put into my console from a test.html page on my local machine that loads jquery:

var url = 'http://google.com';
$.ajax({
  type: 'GET',
  url: url,
  error: function(req, resp) {
    console.log('Error fetching ' + url);

    console.log("req:");
    console.log(req);

    console.log("resp:");
    console.log(resp);    
    },
  success: function(data) {
    console.log("success! here's the data:");
    console.log(data);
  }
});

Your help is much appreciated!

Upvotes: 0

Views: 1464

Answers (1)

Robot Woods
Robot Woods

Reputation: 5687

You cannot just arbitrarily grab other sites due to the Same Origin Policy. You're going to need to build an intermediary on your server that does that data grab (in PHP you could use file_get_contents or curl) and then THAT script (which is on your domain) would be the target of the ajax call

Upvotes: 2

Related Questions