问题
I am attempting to pase a a JSON data using the JQuery getJSON function. The REST query is:
http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%20%3D%20%22london%22&format=json&jsoncallback=?
The script I'm using to parse 'data' to obtain the WOEID value doesnt seem to work below:
$.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=?",
function(data){
console.log("json: " + data);
var datatmp = data;
if(data.results[0]){
var data = filterData(data.results.place[0]);
}
}
);
Can anyone say what I'm doing wrong? link text
回答1:
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.
回答2:
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]);
}
回答3:
You have two key mistakes:
- The correct parameter for specifying the callback in the YQL URL is
callback
rather thanjsoncallback
- The results are to be found in
data.query.results…
rather thandata.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.
回答4:
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"?
来源:https://stackoverflow.com/questions/2604082/using-jquery-getjson-method