jQuery Yahoo Weather API调用失败

时间:2012-06-04 10:33:35

标签: javascript jquery api yahoo weather

我想抓住天气信息。我正在尝试使用jQuery。所以这是我的代码:

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

这似乎有效。但它输出了我

  

Uncaught SyntaxError:意外的令牌:

我认为这是一个JSON验证问题。但是所有在线JSON验证工具都通过了测试。

1 个答案:

答案 0 :(得分:0)

似乎API返回JSON,而不是JSONP数据; getJSON自动尝试解析它,因为JSONP在URL中有一个回调查询:

  

如果网址包含字符串“callback =?” (或类似的,由服务器端API定义),请求被视为JSONP。

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


试试这个:

$(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');
      }
    });
});
相关问题