views:

70

answers:

3

Hi,

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&amp;south=-9.9&amp;east=-22.4&amp;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&amp;format=json&amp;num_of_days=5&amp;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;
      });
    }); 
 });

Thanks!

+1  A: 

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

Matthew Scharley
+3  A: 

It should be as easy as this:

<script type="text/javascript">
  jQuery.getJSON('http://ws.geonames.org/weatherJSON?north=90&amp;south=-9.9&amp;east=-22.4&amp;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"&gt;&lt;/script&gt;
  <script>
    jQuery.getJSON('http://ws.geonames.org/weatherJSON?north=90&amp;south=-9.9&amp;east=-22.4&amp;west=55.2', function(data) {
      jQuery.each(data.weatherObservations, function(i,item){
        jQuery("<p/>").html(item.temperature).appendTo("#para");
      });
    });
  </script>
</body></html>
Mark Eirich
__Except__ that JSON-P requires cooperation from the server. It needs to return JavaScript that calls the callback you specify. So it won't work in this case.
darkporter
redsquare
Hi, I'd really appreciate help with my edited doubted above. Thanks.
rookie
@rookie - see my edited answer. Use the jQuery html() function to put content into the paragraphs, rather than the attr() function.
Mark Eirich
Hi Mark, I'd really appreciate your help with the re-edit. Thanks!
rookie
Okay, got it: To get that last one to work, use this instead of getJSON(): `jQuery.get(url, function(data) { /*...*/ }, 'jsonp');`
Mark Eirich
+2  A: 

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&amp;south=-9.9&amp;east=-22.4&amp;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);
        }
    }
});​
rchern
I'm sorry if my question seems really simple. But I cannot understand why I would need to verify the textstatus? What I'm trying to ask is, what purpose does it serve? Thanks.
rookie
It is a value letting know that if it was a success. Double check, but I think the values are "timeout", "error", "notmodified", "success", "parsererror". If there's an error, you want to know, right?
rchern
Yeh, that makes sense. So it serves the purpose of the try catch block in java. Thank you.
rookie