March 20, 2012

Not able to get the json response

Question by Nirav Gandhi

    var myurl="https://raw.github.com/currencybot/open-exchange-rates/master/latest.json";

    $.ajax(
    {
     url:myurl,
     type:"POST",
     dataType:"JSONP",
     success:function(myObj)
       {
        console.log(myObj);  
       }
   });

I tried using the shorthand getJSON, but console throws me an error “use POST request”.
And using the code above, console states that “Invalid Label”.

Answer by Starx

Use $.getJSON(), it is shorthand function and very efficient too.

var myurl="https://raw.github.com/currencybot/open-exchange-rates/master/latest.json";

$.getJSON(
  myurl,
  function(data) {
      //manipulate your json
});

But for the JSONP request, you should have callback parameter in the URL

As quoted on the documentation[here]

If the URL includes the string “callback=?” (or similar, as defined by the server-side API), the request is treated as JSONP instead.

...

Please fill the form - I will response as fast as I can!