【发布时间】:2017-05-03 11:50:07
【问题描述】:
我有一个从 openweathermap.org 请求天气数据的 api
var requestWeatherData = function(ipData){
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather",
dataType: "json",
data: {
q: ipData.city + ',' + ipData.countryCode,
appid: "cant say"
},
success: function(wthrDetails) {
addWeatherIcon(wthrDetails)
},
});
来自 api 的示例回复如下:
{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}
但是当我尝试通过上面的回复获取天气状态时:
var addWeatherIcon = function(weatherDetails) {
var weatherType = weatherDetails.weather[0].main.toLowerCase();
console.log(weatherType); //<-- outputs "clouds"
console.log(typeof weatherType); //<---- outputs string
console.log(weatherType == "clouds"); // <-- returns false
}
为什么console.log(weatherType == "clouds"); 返回 false?该类是正确的,输出也是正确的,但它仍然返回 false。更奇怪的是,有时当互联网速度很慢时它返回 true?难道是Ajax需要很慢才能检测到它? .这怎么可能?
【问题讨论】:
-
本帖推荐使用JSON.parse?
-
这真的很奇怪......它在codepen中工作......
-
nvm 又说未知天气了...