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.
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).
<!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?
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.
You can't do alert()
on a JSON object.
Instead, try this:
alert(JSON.stringify(data));
or, use console.log
:
console.log(data);
to see each property and its associated value try
function(data){
for(att in data){
console.log(data[att]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With