【问题标题】:Simple Weather API Current Temperature Not Working简单天气 API 当前温度不工作
【发布时间】:2013-02-05 13:32:50
【问题描述】:

我是 JavaScript 和 jquery 的新手...我一直在尝试编写一段简单的代码来使用天气 api 来提醒当前温度。

这是我目前得到的:

 <script src="http://code.jquery.com/jquery-1.9.1.js"/>

 <script>
 var my_city="Washington,USA";
 var my_key="8b0642a6c7133932132002";
 var no_of_days=2;
 // build URI:
 var uri="http://free.worldweatheronline.com/feed/weather.ashx?               q="+my_city+"&key="+my_key+"&format=json&no_of_days="+no_of_days+"&includeLocation=yes";
 // uri-encode it to prevent errors :
 uri=encodeURI(uri); 

 jQuery.get(uri,function(r){
 current_temp_C  =r.data.current_condition[0].temp_C;
 alert(current_temp_C);
 },"json");
 </script>

但是,没有显示任何内容。如果有人能指出我哪里出错了,请告诉我! 谢谢!

【问题讨论】:

标签: jquery json api weather


【解决方案1】:

我假设您最终会进入错误处理程序,并且您只是在代码中处理成功的 ajax 调用。由于您使用的是 jquery > 1.5,因此您可以执行以下操作来检查:

jQuery.get(uri,function(r){
    current_temp_C  =r.data.current_condition[0].temp_C;
     alert(current_temp_C);
},"json").fail(function(jqXHR, ajaxOpts, thrownError){
    alert(thrownError);
});

另外,根据代码,您查询的字符串有很多空格。不要将数据作为自定义查询字符串传递,而是将带有数据的对象文字作为第二个参数添加到$.get

jQuery.get(uri, { key1: 'value1', key2: 'value2' }, function(r){
//etc...

【讨论】:

  • @InfiniDaZa 如果您详细说明“不走运”,我可以尝试帮助您
猜你喜欢
  • 1970-01-01
  • 2021-06-02
  • 2014-08-14
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多