Tom
Tom

Reputation: 4577

jQuery cross domain AJAX request fails with XMLHttpRequest cannot load error

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.

Upvotes: 0

Views: 2355

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93611

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:)).

Upvotes: 1

Related Questions