开发者

need direction regarding jquery and json

开发者 https://www.devze.com 2023-01-06 23:01 出处:网络
I have this url which is JSON webservice http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2

I have this url which is JSON webservice http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2

I need to write a jquery function to access the JSON object from the above url. I am not sure how to proceed with this task. Could someone help me out with starting out the jquery code?

Thanks


Inorder that the HTML appear I removed the "<" for each tag. What I tried doing below is iterate over the items returned via the JSON object. However, I don't seem to get any result. Could some one point out my error in this regard.

body>

div id="para">

/div>

script>

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

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

/script>

/body>

Thank you.


Hi,

I now need to access another web service. I typed out the code on the exact same lines as above but I don't get any output. Could some one help in pointing my mistake?

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.wea开发者_StackOverflow社区ther, function(i, item){
        $("body").append("<p>"+item.date+"</p>");
        if ( i == 3 ) return false;
      });
    }); 
 });

Thanks!


It should be as easy as this:

<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>

Keep in mind, however, that your AJAX call must come from the same domain (ws.geonames.org), since most modern browsers do not allow cross-domain requests. The workaround is to use the JSON-P format instead of pure JSON.

... In response to rookie's edits to his original question, here is a more complete solution:

<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>


To help you read the content that's comes back, put it into http://jsbeautifier.org/ and it will format it so it is readable.

In addition to Mark's answer, you should verify the 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);
        }
    }
});​


Searching for jquery json turned up this page first, which seems to be exactly what you need: jQuery.getJSON()

0

精彩评论

暂无评论...
验证码 换一张
取 消