使用 JQuery getJSON 方法

发布于 2024-08-28 04:38:42 字数 949 浏览 6 评论 0 原文

我正在尝试使用 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]);
           }
         }
       );

任何人都可以说我做错了什么吗? 链接文本

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-09-04 04:38:42

您的代码需要一些调整,这里有一个更新版本:

$.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"]);
              });
          }
      });​

results 对象位于 query 下方,因此您需要先进入那里,上面的代码迭代了 woeid第一个位置返回并提醒他们...这只是一个开始,不确定您最终想要对 woeid 做什么,但希望这能让您开始。 您可以在此处看到上面的代码

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.

漫雪独思 2024-09-04 04:38:42

在这一行中:

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

您检查 results[0] 是否存在,但随后不使用它。我怀疑您的问题可以通过更改为以下内容来解决:

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

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]);
       }
凹づ凸ル 2024-09-04 04:38:42

您有两个关键错误:

  1. 在 YQL URL 中指定回调的正确参数是 callback 而不是 jsoncallback
  2. 结果可以在 data.query 中找到。 results... 而不是 data.results...

另外值得注意的是,YQL 结果返回了一个 data.query.count 值,以便您可以看到返回了多少个结果。

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.

箹锭⒈辈孓 2024-09-04 04:38:42

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"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文