JQuery.getJSON (url, [data], [callback])

To everyone

I am trying to get the exchange rate from Google using jQuery $ .getJSON (). Using the query: " http://www.google.com/ig/calculator?hl=en&q=1USD=?CAD"

returns a simple JSON file: {lhs: "1 U.S. dollar",rhs: "1.03800015 Canadian dollars",error: "",icc: true}

I use the following jquery function to get the Canadian dollar amount .:

$(document).ready(function(){
    $.getJSON("http://www.google.com/ig/calculator?hl=en&q=1USD=?CAD?&label=rhs&format=json&jsoncallback=?",
              function(data){
                  alert(data);
              });
});
</script>

The Fire error displays the correct JSON file, but indicates that an invalid label is being used.

Any help is appreciated.

Bean

+5
source share
3 answers

Google returns pure JSON and does not support JSONP (= JSON wrapped in a callback).

JSONP looks like this:

callbackFunction({json_object: "some_data"})

JSONP- , , JavaScript script -tags . JSON JavaScript script -tags .

Google JSON iGoogle, AJAX ( ), . , ( ).

+7

, JSON. . , Firebug , .

// this fails
jQuery.parseJSON('{lhs: "1 U.S. dollar", rhs: "1.03800015 Canadian dollars", error: "", icc: true}'));

// this works
jQuery.parseJSON('{"lhs": "1 U.S. dollar", "rhs": "1.03800015 Canadian dollars", "error": "", "icc": true}'));
+2

I don’t think Google calculator supports JSONP (which is necessary for cross-domain javascript). Especially yours &jsoncallback=?does nothing.

You need to use a proxy server on your server.

+1
source

All Articles