0

I want to catch weather information. I'm trying to do it with jQuery. So here is my Code:

$(document).ready(function(){
var weatherURL = 'http://weather.yahooapis.com/forecastjson?w=20066287&u=c&callback=?';
$.getJSON(weatherURL, function(data){
    //console.log('done');
}); });

It seems that this is working. But it outputs me

Uncaught SyntaxError: Unexpected token :

I think its an JSON validation problem. But all online JSON validation tool passes the test.

tuna
  • 931
  • 2
  • 10
  • 28
  • You're getting JSON back, not JSONP. http://weather.yahooapis.com/forecastjson?w=20066287&u=c&callback=foobar – Matt Ball Jun 04 '12 at 10:35
  • 1
    check this http://joynag.net/demos/weather now , that JSON is fine and parsing correctly in php, But JS is not being able to parse that. But jsonlint.com says it's valid. So one workaround can be to fetch that using php and pass to JS from backend. I have simulated a ajax request and it's working fine. – Prasenjit Kumar Nag Jun 04 '12 at 11:32

1 Answers1

0

It seems the API returns JSON, not JSONP data; getJSON automatically tries to parse it as JSONP is there's a callback query in the URL:

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

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


Try this:

$(document).ready(function(){
    var weatherURL = 'http://weather.yahooapis.com/forecastjson?w=20066287&u=c';
    $.ajax({
      url: weatherURL,
      dataType: 'json',
      success: function(data) {
        //console.log('done');
      }
    });
});
Jeroen
  • 13,056
  • 4
  • 42
  • 63