简体   繁体   中英

Retrieve the url from a yql result

I have been trying to understand how to return links from yql results, but so far the only thing im getting is [object Object] and i am not getting anywhere so far, could you care to explain to me why is this happening? All I want is to display the results as links.

<div id="funfacts"> 
</div> 
<script> 
function funfacts(o){ 
  var facts = document.getElementById('funfacts'); 
  if(facts){ 
    var data = o.query.results.a; 
    var out = document.createElement('a'); 
    out.className = 'fact'; 
    facts.appendChild(out); 
    out.innerHTML = data.join("<br />"); 
  } 
} 
</script> 
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20href%20from%20html%20where%20url%3D'http%3A%2F%2Fcid-a59bebc211b678a6.office.live.com%2Fbrowse.aspx%2FPublic%2Fhi5'%20and%20xpath%3D'%2F%2Fa%5B%40id%3D%22download%22%5D'%0D%0A&format=json&diagnostics=true&callback=funfacts"></script> 

I hope this example is enough for understanding, all i am trying to do is for it to display the list of urls from that page, however it only returns a list of [object Object] lines.

If you do a console.log on the return object you'll see that results is actually an array with three arrays within it. Thus to get the url you would actually need to do something like o.query.results.a[n].href

results: Object
a: Array[3]
0: Object
href: "http://public.blu.livefilestore.com/y1pXKbPqGSt2TkgDjLyAOoYLWSPBnYLdxVtOHoR4W0sERdCyCncGu4LR8Bg4e81W…"
__proto__: Object
1: Object
2: Object

Here is the jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM