JQuery AJAX and Google Currency Calculator

I have a code that makes an AJAX call to the Google Currency Calculator. Which should theoretically return a JSON array that I can use to get some data related to the exchange rate.

Link:

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

Going to the link shows

{lhs: "1 US dollar", rhs: "6.49148317 Chinese yuan", error: "", icc: true}

My javascript code (I'm tired of this with 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'];);
   }
});

Examination of a fire error is displayed in red with a blank answer.

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

What am I doing wrong?

+3
source share
3

, Google iGoogle 1 . .

+1

As we know, Google has stopped using iGoogle services since November 1, 2013.

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

The following jquery example 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;
}
+1
source

All Articles