使用ajax获取url但由于某种原因它没有

时间:2017-01-11 08:35:20

标签: javascript ajax get

function getCity(city){
 $.ajax({
        url: 'https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + city + '")',
        type: 'GET',
        dataType: 'json',
          success: function (data) {
    var humi = data.query.results.channel.atmosphere.humidity;
    var wind = data.query.results.channel.wind.speed;
    var temp = data.query.results.channel.item.condition.temp;
        temp = Math.floor((temp - 32) * (5 / 9));
        wind = Math.floor(wind*1.609344);
        $('#temp-get').html(temp+"C°");
        $('#humi-get').html(humi+"%");
        $('#wind-get').html(wind+"km/h");
        },
        error: function(){
        $('#temp-get').html("Couldn't get info");
        $('#humi-get').html("Couldn't get info");
        $('#wind-get').html("Couldn't get info");    
        }
    });
}

它始终返回无法获取信息,如果我注释掉错误并删除成功它仍然不会返回任何内容,但如果我使用: $.get('https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + city + '")&format=json', 它返回信息,但它然后我不能使用成功和错误来检查我是否得到了我知道的信息

1 个答案:

答案 0 :(得分:0)

看看jQuery' documentation

$.get的第三个参数是Ajax success.fail()的错误。

所以,你可以传递第三个参数:

$.get('https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + city + '")', 
       {}, 
       function(data){console.log(data);})
  .error(function(){console.log("ERR");})