Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get data from this API link?

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.

like image 215
Charlotte Spencer Avatar asked Nov 09 '12 13:11

Charlotte Spencer


2 Answers

You can't do alert() on a JSON object.

Instead, try this:

alert(JSON.stringify(data));

or, use console.log:

console.log(data);
like image 106
Aamir Avatar answered Oct 06 '22 05:10

Aamir


to see each property and its associated value try

function(data){
for(att in data){
console.log(data[att]);
}
like image 28
Pedro del Sol Avatar answered Oct 06 '22 05:10

Pedro del Sol