Skip to content Skip to sidebar Skip to footer

Open Weather Map Api , Couldn't Get Json, But Getting Jsonp And Couldnt Make Asynchronous Call

Am getting longitude and latitude values from google's webserivce and passing the values to open weather map api to get the temperature values. Code below function getWeatherData(l

Solution 1:

If you're using JQuery then you could use a defer and promise. Something like this:

functiongetWeatherData(latitude, longitude) {
    var temperature = 0;
    var dfd = $.Deferred();
    var url = "http://api.openweathermap.org/data/2.5/weather?lat=";
    url += latitude;
    url += "&lon=";
    url += longitude;
    url += "&cnt=1";
    $.ajax({
        type: "POST",
        dataType: "jsonp",
        url: url + "&callback=?",
        async: false,
        success: function (data) {
            temperature = data.list[0].main.temp;
            alert(temperature);
            dfd.resolve(temperature);
        },
        error: function (errorData) {
            alert("Error while getting weather data :: " + errorData.status);
        }
    });
    return dfd.promise();
}

This will cause it to return the value once temperature has been resolved through the ajax call. I was using a weather API and had this same exact problem.

Post a Comment for "Open Weather Map Api , Couldn't Get Json, But Getting Jsonp And Couldnt Make Asynchronous Call"