【问题标题】:ajax - jquery boolean equality acting funnyajax - jquery 布尔相等表现有趣
【发布时间】: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需要很慢才能检测到它? .这怎么可能?

编辑: 我的密码笔:http://codepen.io/nuclearmachine/full/ZKyrVp

【问题讨论】:

  • 本帖推荐使用JSON.parse?
  • 这真的很奇怪......它在codepen中工作......
  • nvm 又说未知天气了...

标签: jquery json ajax


【解决方案1】:

试试:

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

某处一定有空格。

【讨论】:

    【解决方案2】:

    weatherDetails.weather[0].main.toLowerCase()=="clouds" 它只会返回 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/

    【讨论】:

      【解决方案3】:

      天哪,"thunderstorm" 拼写为 "thunderstom"。现在应用程序有时正确显示的原因是因为其他天气模式拼写正确!我觉得自己很愚蠢,但吸取了教训。现在上面的问题以云为例,但我在我的电脑 GAAH 中测试雷暴。

      【讨论】:

        猜你喜欢
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        • 2015-03-18
        • 2020-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多