【问题标题】:need direction regarding jquery and json需要关于 jquery 和 json 的指导
【发布时间】:2011-03-14 14:01:31
【问题描述】:

我有这个 url,它是 JSON webservice http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2

我需要编写一个 jquery 函数来从上述 url 访问 JSON 对象。 我不确定如何继续执行此任务。有人可以帮助我开始编写 jquery 代码吗?

谢谢


为了显示 HTML,我删除了每个标签的“

正文>

div id="para">

/div>

脚本>

$.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2',函数(数据) {

      $.each(data.weatherObservations, function(i,item){
        $("<p/>").attr("src", item.temperature).appendTo("#para");
        if ( i == 3 ) return false;
      });
    });

/脚本>

/正文>

谢谢。


嗨,

我现在需要访问另一个网络服务。我在与上面完全相同的行上输入了代码,但没有得到任何输出。有人可以帮助指出我的错误吗?

jQuery(document).ready(function() {

  var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=75080&format=json&num_of_days=5&key=ac9c073a8e025308101307';

  jQuery.getJSON(url, function(data) {
      $.each(data.data.weather, function(i, item){
        $("body").append("<p>"+item.date+"</p>");
        if ( i == 3 ) return false;
      });
    }); 
 });

谢谢!

【问题讨论】:

  • 要让 HTML 标签显示在您的问题中,只需将您的代码缩进 4 个空格。
  • 要让最后一个工作,使用它而不是 getJSON():jQuery.get(url, function(data) { /*...*/ }, 'jsonp');
  • 我很想看看你的成品。
  • 谢谢马克!整个产品是一个远程医疗系统,这是迈向认知阶段的模块之一。
  • stackoverflow.com/questions/3240539/… Mark 你能指出我的错误吗?谢谢!

标签: javascript jquery json


【解决方案1】:

应该就这么简单:

<script type="text/javascript">
  jQuery.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2', function(data) {
    // The JSON object is now in the data variable --
    // process it here; for example:
    alert(data.weatherObservations[0].clouds);
  });
</script>

但是请记住,您的 AJAX 调用必须来自同一个域 (ws.geonames.org),因为大多数现代浏览器不允许跨域请求。解决方法是使用 JSON-P 格式而不是纯 JSON。

...针对菜鸟对其原始问题的修改,这里有一个更完整的解决方案:

<html><head></head><body>
  <div id="para"></div>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script>
    jQuery.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2', function(data) {
      jQuery.each(data.weatherObservations, function(i,item){
        jQuery("<p/>").html(item.temperature).appendTo("#para");
      });
    });
  </script>
</body></html>

【讨论】:

  • 除了 JSON-P 需要服务器的配合。它需要返回调用您指定的回调的 JavaScript。所以在这种情况下它不起作用。
  • @darkporter - 它会起作用,getJSON 会附加回调并且 geonames 端点会使用 javascript 函数进行响应,试试 ws.geonames.org/…
  • 您好,非常感谢您对我上面的编辑有疑问的帮助。谢谢。
  • @rookie - 查看我编辑的答案。使用 jQuery html() 函数将内容放入段落中,而不是 attr() 函数。
  • 嗨,马克,非常感谢您对重新编辑的帮助。谢谢!
【解决方案2】:

为了帮助您阅读返回的内容,请将其放入http://jsbeautifier.org/,它将对其进行格式化以便阅读。

除了马克的回答,你应该验证 textStatus

http://jsfiddle.net/VC5c2/

var url = "http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2";

​jQuery.getJSON(url,function(data, textStatus) {
    if (textStatus == "success") {
        for (idx in data.weatherObservations) {
            var wo = data.weatherObservations[idx];
            console.log("Temperature at " + wo.stationName + ":  " + wo.temperature);
        }
    }
});​

【讨论】:

  • 如果我的问题看起来很简单,我很抱歉。但我不明白为什么我需要验证文本状态?我想问的是,它的目的是什么?谢谢。
  • 这是一个让我们知道它是否成功的价值。仔细检查,但我认为这些值是“超时”、“错误”、“未修改”、“成功”、“解析错误”。如果有错误,你想知道,对吧?
  • 是的,这很有道理。所以它服务于 java 中 try catch 块的目的。谢谢。
【解决方案3】:

搜索jquery json首先打开了这个页面,这似乎正是你需要的:jQuery.getJSON()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多