从昨晚开始,我才开始研究 JavaScript 和 jQuery。我正在玩 foursquare API(我已经讨厌 oauth,但这可能会在另一个时间发布另一篇文章),虽然我喜欢这种学习方式,但当你有基本知识时很难。
我的问题很简单,我想从不需要身份验证/授权的 API 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) {
alert(data);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
当我单击警报时,它会向我提供“[object, Object]”,显然这不是我要找的。如何让它显示来自 URL 的数据?
我意识到这是非常基础的(我知道该做什么,只是不知道怎么做)非常感谢任何能帮助我的人。
最佳答案
您不能对 JSON 对象执行 alert()
。
相反,试试这个:
alert(JSON.stringify(data));
或者,使用 console.log
:
console.log(data);
关于javascript - 如何从此 API 链接获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13309137/