2017-05-03 7 views
0

を演技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

+0

この投稿は[JSON.parse](http://stackoverflow.com/a/34744766/6224482)を使用することをお勧めしますか? – Sandman

+0

これは本当に変です....それはcodepenで動作します... – chaosfirebit

+0

nvmその不明な天気を再度... – chaosfirebit

答えて

0

試してみてください。

var weatherType = weatherDetails.weather[0].main.toLowerCase().trim(); 

どこかにスペースが存在する必要があります。

+0

まだ偽.... – chaosfirebit

0

weatherDetails.weather [0] .main.toLowerCase()== "雲" それだけtrueを返します、あなたのコードを確認してください

例のフィドルは

$(document).ready(function(){ 
var details={ 
    "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 
}; 
alert(details.weather[0].main.toLowerCase()=="clouds"); 
}); 

https://jsfiddle.net/fdxuf7qn/

ことを確認しています
0

ああ私の神、"thunderstorm"は、"thunderstom"という綴りです。アプリケーションが正しく表示されていた理由は、他の天気パターンが正しく綴られているためです。私は馬鹿だと感じるが、教訓は学んだ。今、上記の質問には雲がありますが、コンピュータGAAHの雷雨をテストしていました。