admin管理员组文章数量:1313347
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(latitude, longitude) {
var temperature = 0;
var url = ".5/weather?lat=";
url = url + latitude;
url = url + "&lon=";
url = url + longitude;
url = url + "&cnt=1";
$
.ajax({
type : "POST",
dataType : "jsonp",
url : url + "&callback=?",
async : false,
success : function(data) {
temperature = data.list[0].main.temp ;
alert (temperature);
},
error : function(errorData) {
alert("Error while getting weather data :: "+errorData.status);
}
});
return temperature;
So for this URL
.1/find/city?lat=22.572646&lon=88.36389500000001&cnt=1
Am getting the below JSON response properly in the browser
{
"message": 0.016,
"cod": "200",
"calctime": "",
"cnt": 1,
"list": [{
"id": 1275004,
"name": "Kolkata",
"coord": {
"lon": 88.36972,
"lat": 22.569719
},
"distance": 0.999,
"main": {
"temp": 301.15,
"pressure": 998,
"humidity": 88,
"temp_min": 301.15,
"temp_max": 301.15
},
"dt": 1371217800,
"wind": {
"speed": 3.1,
"deg": 150
},
"clouds": {
"all": 40
},
"weather": [{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50n"
}]
}]
}
But while trying to hit the same using jQuery's ajax, I have no option except to get the values as JSONP, unable to get it as JSON
Since am unable to get JSON response, am not able to make the call asynchronous.
I need to make asynchronous false. Because of this every time, the value temperature is set to 0 and am not able to get the actual temperature value which i get from the ajax call
Please help
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(latitude, longitude) {
var temperature = 0;
var url = "http://api.openweathermap/data/2.5/weather?lat=";
url = url + latitude;
url = url + "&lon=";
url = url + longitude;
url = url + "&cnt=1";
$
.ajax({
type : "POST",
dataType : "jsonp",
url : url + "&callback=?",
async : false,
success : function(data) {
temperature = data.list[0].main.temp ;
alert (temperature);
},
error : function(errorData) {
alert("Error while getting weather data :: "+errorData.status);
}
});
return temperature;
So for this URL
http://api.openweathermap/data/2.1/find/city?lat=22.572646&lon=88.36389500000001&cnt=1
Am getting the below JSON response properly in the browser
{
"message": 0.016,
"cod": "200",
"calctime": "",
"cnt": 1,
"list": [{
"id": 1275004,
"name": "Kolkata",
"coord": {
"lon": 88.36972,
"lat": 22.569719
},
"distance": 0.999,
"main": {
"temp": 301.15,
"pressure": 998,
"humidity": 88,
"temp_min": 301.15,
"temp_max": 301.15
},
"dt": 1371217800,
"wind": {
"speed": 3.1,
"deg": 150
},
"clouds": {
"all": 40
},
"weather": [{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50n"
}]
}]
}
But while trying to hit the same using jQuery's ajax, I have no option except to get the values as JSONP, unable to get it as JSON
Since am unable to get JSON response, am not able to make the call asynchronous.
I need to make asynchronous false. Because of this every time, the value temperature is set to 0 and am not able to get the actual temperature value which i get from the ajax call
Please help
Share Improve this question asked Jun 14, 2013 at 15:07 ArunArun 3,70012 gold badges67 silver badges118 bronze badges 9- 1 Wele to the wonderful world of async! You need to return the value using a callback. – SLaks Commented Jun 14, 2013 at 15:08
- 4 JSONP cannot be POST, nor synchronous. – SLaks Commented Jun 14, 2013 at 15:09
- I need to make asynchronous false and this stops me to return the actual temperature value. Can you please help me saying how to do it actually ? – Arun Commented Jun 14, 2013 at 15:11
- ^^^ What SLaks said! JSONP isn't "really" ajax, it inserts a script tag instead, so GET and async is the only way to do that, setting anything else in $.ajax is just ignored. – adeneo Commented Jun 14, 2013 at 15:12
- I couldn't understand. really sorry for that :( . so now how i will get the actual temperature value. The function always returns the value 0 as it is not waiting for the ajax call – Arun Commented Jun 14, 2013 at 15:17
1 Answer
Reset to default 8If you're using JQuery then you could use a defer and promise. Something like this:
function getWeatherData(latitude, longitude) {
var temperature = 0;
var dfd = $.Deferred();
var url = "http://api.openweathermap/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.
本文标签:
版权声明:本文标题:javascript - Open weather map API , couldn't get JSON, but getting JSONP and couldnt make asynchronous call - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741946289a2406433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论