【问题标题】:Using ajax to get url but for some reason it doesnt使用 ajax 获取 url 但由于某种原因它没有
【发布时间】:2017-01-11 08:35:19
【问题描述】:
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) 阅读您写问题的方框旁边的代码格式说明。 (2) 在浏览器中打开开发者工具,查看控制台。可能会出现一条错误消息,告诉您问题所在。 (3) 在浏览器中打开开发者工具并查看 Network 选项卡:这将告诉您浏览器究竟在请求什么以及服务器返回什么。

标签: javascript ajax get


【解决方案1】:

看看 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");})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2014-12-27
    相关资源
    最近更新 更多