【问题标题】:getJSON no output?getJSON 没有输出?
【发布时间】:2012-05-09 15:02:21
【问题描述】:

我一生都无法弄清楚为什么这不起作用?

<div class="clouds">

</div>

<div class="station">

</div>

<script type="text/javascript">
$.getJSON("http://api.geonames.org/findNearByWeatherJSON?lat=53.36652&lng=-2.29855&username=jolones&callback=?",
function(data){

          var clouds = data.weatherObservation.clouds;
          var station = data.weatherObservation.stationName;

      jQuery.(".clouds").html(clouds);
      jQuery.(".station").html(station);

      });

非常感谢

【问题讨论】:

  • 使用console.log(data); 调试您的响应。
  • 通过jslint运行这样的问题会多次隔离你的错误,只是说。 :)

标签: javascript jquery json api weather-api


【解决方案1】:
jQuery . (".clouds").html(clouds);
jQuery . (".station").html(station);

你的意思是那里有那些点吗?应该是

jQuery(".clouds").html(clouds);
jQuery(".station").html(station);

【讨论】:

  • 我讨厌自己,但我是你的忠实粉丝。非常感谢。
  • 没问题!虽然看起来@Mike 所说的也是一个问题。
  • 我在这里看不到“麦克”问题:jsfiddle.net/YxcWy/1 - 曼彻斯特散落的云朵 eh
  • 对我来说在曼彻斯特就像“破云”。不知道有破云这种东西。
【解决方案2】:

浏览器安全策略阻止您将 json ( xmlhttpReqeusts ) 发送到另一个域。不过,您应该查看 JSONP 的工作原理,如果 geonames.org 提供 jsonp 方法(jsonp 将允许您这样做并调用另一个域),这可能很有用。

另一种选择是创建一个远程调用 api 的本地 php 脚本。不禁止 PHP 调用远程 api,因此您可以使用 php 检索 json 结果,然后使用 javascript 从本地(在同一域上)php 程序中检索结果。

【讨论】:

  • 查找并看到它以&amp;callback=? 结尾,很可能是一个 JSONP 调用。
  • jQuery 1.5 开始默认将 URL 中带有 ?callback= 的任何 getJSON 请求视为 JSONP 请求,因此该请求仍然有效api.jquery.com/jQuery.getJSON/#jsonp
  • @steveukx 很高兴知道,我必须确保我运行的是最新版本
猜你喜欢
  • 1970-01-01
  • 2015-12-21
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多