Using JQuery getJSON method

落花浮王杯 提交于 2019-12-05 20:46:01

Your code needed a few tweaks, here's an updated version:

$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
      "q=select%20woeid%20from%20geo.places%20where%20text%20%3D%20%22"+
      "london"+
      "%22&format=json&jsoncallback=json",
      function(data){
          if(data.query.results){
              $.each(data.query.results.place, function(i, v) {
                  console.log("woeid #" + i + ": " + v["woeid"]);
              });
          }
      });​

The results object is beneath query, so you need to go into there first, the above code iterates through the woeid's of the first place returned and alerts them...it's just a starter, not sure what you ultimately wanted to do with the woeid but hopefully this will get you started. You can see the above code working here.

In this line:

      if(data.results[0]){
        var data = filterData(data.results.place[0]);
       }

You check to see if results[0] exists but then you don't use it. I suspect your problem would be fixed by changing to this:

      if(data.results[0]){
        var data = filterData(data.results[0].place[0]);
       }

You have two key mistakes:

  1. The correct parameter for specifying the callback in the YQL URL is callback rather than jsoncallback
  2. The results are to be found in data.query.results… rather than data.results…

Also it is worth noting that there is a data.query.count value returned with the YQL results so you can see how many results were returned.

I have a question: can you access that URL (http://query.yahooapis.com/...) even if it's not in your domain? Doesn't that violate the "same origin policy"?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!