cdwyer
cdwyer

Reputation: 697

Pulling simple weather variable (JSON/API)

I want to pull a simple "current weather" status from the following API URL, to integrate into a website:

http://free.worldweatheronline.com/feed/weather.ashx?q=67554&format=json&num_of_days=2&key=794496e1c2020558131802

I'm looking for one of two solutions for a problem. Either some insight as to why this code won't return a simple "Testing." (which it does, if I remove the "$.getJSON" line). Or the full-blown jQuery I need to include, for the current_conditions > temp_F.

<script>
    $(document).ready(function(){
      $.getJSON(
                'http://free.worldweatheronline.com/feed/weather.ashx?q=67554&format=json&num_of_days=2&key=794496e1c2020558131802',
                function(data) {
                  var output="Testing.";
                  document.getElementById("weather").innerHTML=output;   
                });
    });
</script>

Thanks for your help in advance; I really appreciate it!

Upvotes: 0

Views: 231

Answers (1)

Sang Suantak
Sang Suantak

Reputation: 5265

It's because of Same Origin Policy. You should use jsonp & change your code to this:

$(document).ready(function () {
    $.ajax({
        url: 'http://free.worldweatheronline.com/feed/weather.ashx?q=67554&format=json&num_of_days=2&key=794496e1c2020558131802',
        type: "GET",
        dataType: "jsonp",
        success: function (data) {
            console.log(data); //to see that data is indeed returned
            var output = "Testing.";
            $("#weather").html(output);
        }
    });
});

Upvotes: 3

Related Questions