3

I have some code that performs an AJAX call to the google currency calculator. Which in theory should return a JSON array that i can ten use to get some exchange rate related data.

The Link is:

http://www.google.com/ig/calculator?hl=en&q=1USD=?CNY

Going to the link shows

{lhs: "1 U.S. dollar",rhs: "6.49148317 Chinese yuan",error: "",icc: true}

My javascript code (I tired this with both POST and GET):

jQuery.ajax({
    type: "GET",
    url: "http://www.google.com/ig/calculator",
    data: "hl=en&q=1USD=?CNY",
    success: function(msg) {
        var currency = $.parseJSON(msg);
        alert (currency ['rhs'];);
   }
});

Examining fire bug shows in red with an empty response

GET http://www.google.com/ig/calculator?hl=en&q=1USD=?CNY 200 OK 255ms

What am I doing wrong?

Maro
  • 4,065
  • 7
  • 33
  • 34

3 Answers3

3

You can't perform cross domain requests with jQuery. You need to use JSONP to perform this request. These links might help:

http://api.jquery.com/jQuery.getJSON/#jsonp

jsonp with jquery

JSONP requests are not subject to same-origin policy restrictions.

Community
  • 1
  • 1
Mark Costello
  • 4,334
  • 4
  • 23
  • 26
1

heard google has stopped services from iGoogle from Nov 1st.. the link no longer works.

rajesh_kw
  • 1,572
  • 16
  • 14
1

As we know google has stopped services from iGoogle from Nov 1st/2013..

But we can use https://www.google.com/finance/converter to get the real time data.

Following example of jquery will work for you.

    function CurrencyConvetor(amount, from, to) {
    var result = '';
    var url = "https://www.google.com/finance/converter?a=" + amount + "&from=" + from + "&to=" + to;
    $.ajaxSetup({async: false});
    $.get(url,
        function (data) {
            var startPos = data.search('<div id=currency_converter_result>');
            var endPos = data.search('<input type=submit value="Convert">');
            if (startPos > 0) {
                result = data.substring(startPos, endPos);
                result = result.replace('<div id=currency_converter_result>', '');
                result = result.replace('<span class=bld>', '');
                result = result.replace('</span>', '');
            }
    })
    return result;
}
Jigarb1992
  • 828
  • 2
  • 18
  • 41