使用 JQuery getJSON 方法
我正在尝试使用 JQuery getJSON 函数来传递 JSON 数据。 REST 查询是:
http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%20%3D%20%22london%22&format=json&jsoncallback=?
我用来解析“数据”以获取 WOEID 值的脚本在下面似乎不起作用:
$.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]);
}
}
);
任何人都可以说我做错了什么吗? 链接文本
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码需要一些调整,这里有一个更新版本:
results
对象位于query
下方,因此您需要先进入那里,上面的代码迭代了 woeid第一个位置返回并提醒他们...这只是一个开始,不确定您最终想要对woeid
做什么,但希望这能让您开始。 您可以在此处看到上面的代码。Your code needed a few tweaks, here's an updated version:
The
results
object is beneathquery
, 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 thewoeid
but hopefully this will get you started. You can see the above code working here.在这一行中:
您检查
results[0]
是否存在,但随后不使用它。我怀疑您的问题可以通过更改为以下内容来解决:In this line:
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:您有两个关键错误:
callback
而不是jsoncallback
data.query 中找到。 results...
而不是data.results...
另外值得注意的是,YQL 结果返回了一个
data.query.count
值,以便您可以看到返回了多少个结果。You have two key mistakes:
callback
rather thanjsoncallback
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.我有一个问题:您可以访问该 URL (http://query.yahooapis.com/...) 即使它不在您的域中?这不违反“同源政策”吗?
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"?