JQuery 3.x对象不支持属性或方法“成功”

时间:2019-03-15 15:58:28

标签: jquery yahoo-weather-api

我正在使用Yahoo Weather API。它与JQuery 1.x一起正常工作。问题出在JQuery3.x。我收到此错误:对象不支持属性或方法“成功”。我可以使用什么代替.success?

我根据文档尝试了.done,但是它没有显示任何数据。

https://api.jquery.com/deferred.done/

$(document).ready(function(){
  var city = "Erie, PA";
  var searchtext = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + city + "') and u='f'"
  $.getJSON("https://query.yahooapis.com/v1/public/yql?q=" + searchtext + "&format=json").success(function(data){
      $('#weather-temp').html(data.query.results.channel.item.condition.temp + "°F");
      $("#weather-title").text(data.query.results.channel.title);
      $("#weather-text").text(data.query.results.channel.item.condition.text);
      $("#weather-speed").text("Wind: " + data.query.results.channel.wind.speed + " mph");
      $("#weather-sunset").text("Sunset: " + data.query.results.channel.astronomy.sunset);
      var iconUrl = 'https://s.yimg.com/zz/combo?a/i/us/we/52/'
      var weatherCode = data.query.results.channel.item.condition.code;
      $(".weather_icon").attr('src', iconUrl + weatherCode + '.gif');
  });
});

2 个答案:

答案 0 :(得分:2)

对于getJSON,成功回调可以作为第二个参数传递。

参考http://api.jquery.com/jQuery.getJSON/

$.getJSON("https://query.yahooapis.com/v1/public/yql?q=" + searchtext + "&format=json", function(data){
      $('#weather-temp').html(data.query.results.channel.item.condition.temp + "°F");
      $("#weather-title").text(data.query.results.channel.title);
      $("#weather-text").text(data.query.results.channel.item.condition.text);
      $("#weather-speed").text("Wind: " + data.query.results.channel.wind.speed + " mph");
      $("#weather-sunset").text("Sunset: " + data.query.results.channel.astronomy.sunset);
      var iconUrl = 'https://s.yimg.com/zz/combo?a/i/us/we/52/'
      var weatherCode = data.query.results.channel.item.condition.code;
      $(".weather_icon").attr('src', iconUrl + weatherCode + '.gif');
  });

答案 1 :(得分:0)

看这里:http://api.jquery.com/jQuery.ajax/

.done(function() {
  //do stuff
});