0

When sending a GET request to a server why is www.codecademy.com working as the url but not other url's such as www.google.com?

I have a button on my page that when I press sends a get request. The code below works, but why can't I just substitute it for any other url and get some data back? Just want to work on sending GET requests and parsing the data.

$(document).ready(function(){
  $("button").click(function(){
    $.get("http://www.codecademy.com/",function(data,status){
      alert("Data: " + data + "\nStatus: " + status);
    });
  });
});

1 Answers1

4

Check the error returned:

XMLHttpRequest cannot load http://www.google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access

In a nutshell, this is to do with CORS

This SO post may help
Origin is not allowed by Access-Control-Allow-Origin

... by 'help' - I mean help to understand, not help to GET request google from client side Javascript.

You could of course charm the 'Security Princess' of google into opening it up for you, but I highly doubt that's an option ;-)

Community
  • 1
  • 1
Alex
  • 37,502
  • 51
  • 204
  • 332
  • Gotcha. Alex, where can I go to practice sending get requests to a server and parsing through data? – Stack Overflow 421 Oct 05 '14 at 20:15
  • Google has a lot of APIs like [Youtube](https://developers.google.com/youtube/), [Google Maps](https://developers.google.com/maps/), which use json you can parse and play with. – blex Oct 05 '14 at 20:18
  • ... sorry, requestb.in also has same issue - I would knock up a simple node server and run it on localhost? – Alex Oct 05 '14 at 20:18