を演技jqueryのブール平等私はopenweathermap.orgアヤックス - 面白い
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の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");
returはありませんn偽ですか?クラスは正しいですし、出力も同様ですが、それでもfalseが返されます。さらに奇妙なことは、インターネットが遅いときに真実を返します。それを検出するためには、Ajaxを遅くする必要がありますか? 。これはどのように可能ですか?
EDIT: 私のコードペン:http://codepen.io/nuclearmachine/full/ZKyrVp
この投稿は[JSON.parse](http://stackoverflow.com/a/34744766/6224482)を使用することをお勧めしますか? – Sandman
これは本当に変です....それはcodepenで動作します... – chaosfirebit
nvmその不明な天気を再度... – chaosfirebit