javascript 如何从此 API 链接获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13309137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get data from this API link?
提问by Charlotte Spencer
I have just started looking into JavaScript and jQuery as of last night. I'm playing with the foursquare API (I already hate oauth but that might make for another post at another time) and it's hard when you have rudimentary knowledge, though I like this way of learning.
从昨晚开始,我刚刚开始研究 JavaScript 和 jQuery。我正在使用foursquare API(我已经讨厌oauth,但这可能会在其他时间写另一篇文章)并且当您拥有基本知识时很难,尽管我喜欢这种学习方式。
My question is really simple, I want to get data from an API URL that requires no authentication/authorisation. I then just want to display it (in my code I've made it display as an alert onclick).
我的问题非常简单,我想从不需要身份验证/授权的 API URL 获取数据。然后我只想显示它(在我的代码中,我已将其显示为警报 onclick)。
<!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>
When I click on the alert it feeds me "[object, Object]", obviously this is not what I am looking for. How do I get it to display the data from the URL?
当我点击警报时,它会为我提供“[对象,对象]”,显然这不是我要找的。如何让它显示来自 URL 的数据?
I realise this is so incredibly basic (I know what to do, just not how to do it) so huge thanks to anyone who can help me.
我意识到这是非常基本的(我知道该怎么做,只是不知道怎么做)非常感谢任何可以帮助我的人。
回答by Aamir
You can't do alert()
on a JSON object.
你不能alert()
在 JSON 对象上做。
Instead, try this:
相反,试试这个:
alert(JSON.stringify(data));
or, use console.log
:
或者,使用console.log
:
console.log(data);
回答by Pedro del Sol
to see each property and its associated value try
要查看每个属性及其关联的值,请尝试
function(data){
for(att in data){
console.log(data[att]);
}
回答by Nikhil Bhanda
Just write alert(data.tosource());
只需写 alert(data.tosource());
it seems this is what you are looking for
看来这就是你要找的