未捕获的TypeError:无法读取属性'结果'未定义的 - YQL

时间:2016-10-12 17:21:14

标签: javascript jquery html-framework-7

我尝试从Yahoo YQL获取天气数据。请求有效,但它停止工作......

f,ax=plt.subplots(1)

ax.plot(  ...  )

def legend_alpha(ax,newalpha=1.0):
    #sets alpha of legends to some value
    #this would be easier if deepcopy worked on handles, but it doesn't 
    handles,labels=ax.get_legend_handles_labels()
    alphass=[None]*len(handles) #make a list to hold lists of saved alpha values
    for k,handle in enumerate(handles): #loop through the legend entries
        alphas=[None]*len(handle) #make a list to hold the alphas of the pieces of this legend entry
        for i,h in enumerate(handle): #loop through the pieces of this legend entry (there could be a line and a marker, for example)
            try: #if handle was a simple list of parts, then this will work
                alphas[i]=h.get_alpha()
                h.set_alpha(newalpha)
            except: #if handle was a list of parts which themselves were made up of smaller subcomponents, then we must go one level deeper still.
                #this was needed for the output of errorbar() and may not be needed for simpler plot objects
                alph=[None]*len(h) 
                for j,hh in enumerate(h):
                    alph[j]=hh.get_alpha() #read the alpha values of the sub-components of the piece of this legend entry
                    hh.set_alpha(newalpha)
                alphas[i]=alph #save the list of alpha values for the subcomponents of this piece of this legend entry
        alphass[k]=alphas #save the list of alpha values for the pieces of this legend entry
    leg=ax.legend(handles,labels) #create the legend while handles has updated alpha values
    for k,handle in enumerate(handles): #loop through legend items to restore origina alphas on the plot
        for i,h in enumerate(handle): #loop through pieces of this legend item to restore alpha values on the plot
            try: 
                h.set_alpha(alphass[k][i])
            except:
                for j,hh in enumerate(h): #loop through sub-components of this piece of this legend item to restore alpha values
                    hh.set_alpha(alphass[k][i][j])
    return leg

leg=legend_alpha(ax)
leg.draggable()

结果是:

  

未捕获的TypeError:无法读取属性'结果'未定义的

有什么问题?

由于

2 个答案:

答案 0 :(得分:1)

使用dataType:'json'

var woeid = 455827; // example id
var yql = encodeURIComponent('select * from weather.forecast where woeid = "' + woeid + '"and u="c"');
$.ajax({
  dataType: 'json',
  url: 'https://query.yahooapis.com/v1/public/yql?q=' + yql + '&format=json',
  timeout: 5000,
  //Sucesso
  success: function(data) {
    console.log(data);
    var resultados = data.query.results.channel;

    var graus = resultados.item.condition.temp;
    var cod_condicao = resultados.item.condition.code;
    var nascer = resultados.astronomy.sunrise;
    var poente = resultados.astronomy.sunset;
    var umidade = resultados.atmosphere.humidity + '%';
    var vento = resultados.wind.speed + 'KM/H'
    var forecast = resultados.item.forecast;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

您可以将$.getJSON()与网址

一起使用
"https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid='45‌​5827' and u='c'&format=json&diagnostics=true&callback="

"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D'45%E2%80%8C%E2%80%8B5827'%20and%20u%3D'c'&format=json&diagnostics=true&callback="

&#13;
&#13;
$.getJSON("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D'45%E2%80%8C%E2%80%8B5827'%20and%20u%3D'c'&format=json&diagnostics=true&callback=")
  .then(function(data) {
    var resultados = data.query.results.channel;
    var graus = resultados.item.condition.temp;
    var cod_condicao = resultados.item.condition.code;
    var nascer = resultados.astronomy.sunrise;
    var poente = resultados.astronomy.sunset;
    var umidade = resultados.atmosphere.humidity + '%';
    var vento = resultados.wind.speed + 'KM/H'
    var forecast = resultados.item.forecast;
    console.log(resultados, graus, cod_condicao
                , nascer, poente, umidade, vento, forecast);
  })
  .fail(function(jqxhr, textStatus, errorThrown) {
     console.log(errorThrown)
  })
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;