带有多个URL的get.JSON [英] get.JSON with multiple URLs

查看:106
本文介绍了带有多个URL的get.JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用get.JSON来从Foursquare API获取数据(确切地说是venue_history,无需授权)。我能够从多个URL获取json数据。我被告知我这样做的方式并不是一种非常有效的方法,我的同事提到了将URL放入数组并添加计数器以便分别解析每个URL的问题?我想使用每个URL中的数据来显示它(不是在那个阶段,还有很长的路要走!)。

I am working with get.JSON right now to fetch data from the Foursquare API (venue_history to be exact, no authorisation needed). I am able to get json data from multiple URLs. I have been told the way I have done it is not a very effective way to do so, my colleague mentioned something about putting the URLs into an array and adding a counter so it parsed each URL separately? I want to use the data from each URL to visualise it (not at that stage yet, long way to go!).

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.getJSON('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108',
    function(data){
      console.log(data);

   $.getJSON('https://api.foursquare.com/v2/users/1050511?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121109',
   function(data){
      console.log(data);

   $.getJSON('https://api.foursquare.com/v2/users/1050511?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121109',
   function(data){
      console.log(data);

   $.getJSON('https://api.foursquare.com/v2/users/1050511?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121109',
   function(data){
      console.log(data);


          });
        });
      });
    });
  });
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

我的问题是,我怎么能以更有效的方式做到这一点?

My question is, how can I do this is in a more efficient way?

非常感谢!

这是我的新代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function () {
  $("button").click(function GetJSONResult(url)
{
  $.getJSON(url,
   function(data){
    console.log(data);
    });
} 

GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');


});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

它一直在说意外的标识符,我相信这是因为我把所有的GetJSONResult在错误的地方。当它位于代码底部而不是函数内时,它告诉我GetJSONResult未定义?任何帮助表示赞赏。

It keeps on saying "unexpected identifier", I believe this is because I put all the "GetJSONResult" in the wrong place. When it was at the bottom of the code not within the function it was telling me GetJSONResult was undefined? Any help appreciated.

推荐答案

只需创建一个函数

function GetJSONResult(url)
{
  $.getJSON(url,
  function(data){
    console.log(data);
   });
}

并使用

GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');

或者如果你想使用你帖子中提到的数组:

Or if you want to use an array like mentioned in your post:

var urls = ['https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108',
'https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108',
'https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108']

for (var i=0;i<urls.length;i++)
{ 
    GetJSONResult(urls[i]);
}

在您发布的脚本块中,它看起来像这样

In the script block you posted it would look like this

 <script type="text/javascript">
 $(document).ready(function () {
 $("button").click(function(){
   GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
   GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
   GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
   GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
   });
});


function GetJSONResult(url)
{
   $.getJSON(url,
    function(data){
     console.log(data);
    });
}   
 </script>

这篇关于带有多个URL的get.JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆