我现在正在使用get.JSON来从Foursquare API获取数据(确切地说是venue_history,无需授权)。我能够从多个URL获取json数据。我被告知我这样做的方式不是一种非常有效的方法,我的同事提到了将URL放入数组并添加计数器以便分别解析每个URL的问题?我想使用每个URL中的数据来显示它(不是在那个阶段,还有很长的路要走!)。
这是我的代码:
<!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>
我的问题是,我怎样才能以更有效的方式做到这一点?
非常感谢!
这是我的新代码:
<!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未定义?任何帮助表示赞赏。
答案 0 :(得分:1)
只需创建一个功能
function GetJSONResult(url)
{
$.getJSON(url,
function(data){
console.log(data);
});
}
并使用
调用它GetJSONResult('https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=2ZO1PQOUAD5SXRAJOLZVH53RBQ1EB2C23FE2GUZLJYQUJ3SY&v=20121108');
或者如果您想使用帖子中提到的数组:
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]);
}
在您发布的脚本块中,它看起来像这样
<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>