0

I thought I was doing this right but I'm getting the following error in Chrome's JavaScript console:

XMLHttpRequest cannot load https://www.google.com/ig/calculator?hl=en&q=1USD=?JPY. 
Origin  is not allowed by Access-Control-Allow-Origin. 

Any way I try and load the result through JQuery throws this error.

$.ajax({
    type: 'GET',
    url: 'https://www.google.com/ig/calculator?hl=en&q=1USD=?JPY',
    crossDomain: true
})
.fail(function() { alert('error'); })
.success(function(data) { alert(data); })
;
$.get('https://www.google.com/ig/calculator?hl=en&q=1USD=?JPY', function(data) {
    alert(data);
});
$("#test").load('https://www.google.com/ig/calculator?hl=en&q=1USD=?JPY', function(data) {
    alert(data);
});

Even specifying crossDomain to true the AJAX call still fails.

Any idea why or is there a better way to retrieve the results?

Thanks.

Tom
  • 4,467
  • 17
  • 59
  • 91
  • 3
    Because it would appear Google does not allow it. – epascarello Sep 13 '13 at 13:06
  • 1
    possible duplicate of [JQuery ajax cross domain](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – nfechner Sep 13 '13 at 13:07
  • @epascarello : Google does allow it. – Tom Sep 13 '13 at 13:15
  • 2
    @Tom Why do you think Google allows it? Is it in some documentation somewhere? I'm looking at the HTTP response headers, and it looks pretty clear that Google does not allow it, because they don't send back an `Access-Control-Allow-Origin` response header, which is the mechanism by which Google allows cross-origin access to their pages. – apsillers Sep 13 '13 at 13:30
  • @apsillers if I use JSON I get this response https://www.google.com/ig/calculator?hl=en&q=1USD=?JPY&callback=jQuery1710681886202422902_1379078557497&_=1379078559367 which seems to be valid – Tom Sep 13 '13 at 13:34
  • Ah ok. Will try a different solution, page is in ASP so we'll see if I can get a response using that. – Tom Sep 13 '13 at 13:41
  • 1
    +1 the downvote seems harsh when the question is complete with code and is a common issue – iCollect.it Ltd Sep 13 '13 at 14:11

1 Answers1

1

Your best bet is to create a REST/JSON service on your own web server that simply returns the remote results from Google's API.

That way any cross-domain problems are avoided and you have more control over future changes to the API (you can for example reformat the content to suit your own nefarious uses:)).

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202