0
<script type="text/javascript" src="http://static.stackmob.com/js/2.5.3-crypto-sha1-hmac.js"></script>
<script type="text/javascript">
$j.ajax({
        dataType : 'json',
        url : "/api/core/v2/groups/", //or any other rest call here
        beforeSend: function(xhr) {
            var bytes = Crypto.charenc.Binary.stringToBytes('username' + ":" + 'password');
                    var base64 = Crypto.util.bytesToBase64(bytes);
            xhr.setRequestHeader("Authentication", "Basic " + base64);
        },
        success: function(data, status, xhr) {
            console.log("success!");
            console.log(data);
                        console.log(data[0]);
        }
    });

The output of data is

Object
    data: Array[25]
        0: Object
            contentTypes: Array[4]
            creationDate: "2012-05-31T20:21:20.532+0000"
            creator: Object
            description: "A discussion group for anyone interested in amateur radio."
            displayName: "amd-amateur-radio-forum"
            followerCount: 5
            groupType: "OPEN"
            id: 1133
            memberCount: 3
            modificationDate: "2012-06-04T21:23:07.078+0000"
            name: "AMD Amateur Radio Forum"
            resources: Object
            type: "group"
            viewCount: 62
            __proto__: Object
        1: Object
        2: Object
        3: Object

but when I try to output data[0] or data[1] they all come back as undefined. I am not sure how to deal with this JSON object.

The output of alert(data) is

[Object object]

so I am assuming it is a properly formatted JSON object.

Here is the output JSON.stringify(data)

{"data":[{"contentTypes":["discussions","documents","blog","projects"],"memberCount":3,"creator":{"name":"firstname lastname","level":{"name":"Level I","description":"null","points":7,"resources":{"image":{"ref":"http://connect-dev.amd.com/api/core/v2/images/status/statusicon-47.gif","allowed":["GET"]}}},"username":"dkyle","email":"[email protected]","firstName":"firstname","lastName":"lastname","resources":{"self":{"ref":"http://connect-dev.amd.com/api/core/v2/users/4183","allowed":["GET"]},"avatar":{"ref":"http://connect-dev.amd.com/api/core/v2/users/4183/avatar","allowed":["GET"]}},"id":4183},"groupType":"OPEN","name":"AMD Amateur Radio Forum","displayName":"amd-amateur-radio-forum","description":"A discussion group for anyone interested in amateur radio.","followerCount":5,"resources":{"projects":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1133/projects","allowed":["GET"]},"invites":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1133/invites","allowed":["POST"]},"documents":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1133/documents","allowed":["GET","POST"]},"html":{"ref":"http://connect-dev.amd.com/groups/amd-amateur-radio-forum","allowed":["GET"]},"self":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1133","allowed":["GET"]},"blog":{"ref":"http://connect-dev.amd.com/api/core/v2/blogs/1368","allowed":["GET"]},"members":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1133/members","allowed":["GET"]},"discussions":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1133/discussions","allowed":["GET","POST"]}},"id":1133,"type":"group","creationDate":"2012-05-31T20:21:20.532+0000","modificationDate":"2012-06-04T21:23:07.078+0000","viewCount":62},{"contentTypes":["discussions","documents","blog","projects"],"memberCount":34,"creator":{"name":"firstname lastname","level":{"name":"Level I","description":"null","points":24,"resources":{"image":{"ref":"http://connect-dev.amd.com/api/core/v2/images/status/statusicon-47.gif","allowed":["GET"]}}},"username":"username","email":"[email protected]","firstName":"firstname","lastName":"lastname","resources":{"self":{"ref":"http://connect-dev.amd.com/api/core/v2/users/4291","allowed":["GET"]},"avatar":{"ref":"http://connect-dev.amd.com/api/core/v2/users/4291/avatar","allowed":["GET"]}},"id":4291},"groupType":"OPEN","name":"AMD Community Corps Connect (Matching Gifts, GIVE and Volunteerism)","displayName":"amd-community-corps-connect-matching-gifts-give-and-volunteerism","description":"Through this community, employees are be able to support the causes they care about most in ways thev have never been able to before. Not only that, employees will be able to connect and network with fellow AMD employees in a way that allows everyone connected to each other to volunteer together, while providing the ability to track volunteer hours, make charitable contributions and request a match all in the same tool.  \r\n\r\nhttp://amd.yourcause.com \r\n\r\n","followerCount":55,"resources":{"projects":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1074/projects","allowed":["GET"]},"invites":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1074/invites","allowed":["POST"]},"documents":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1074/documents","allowed":["GET","POST"]},"html":{"ref":"http://connect-dev.amd.com/groups/amd-community-corps-connect-matching-gifts-give-and-volunteerism","allowed":["GET"]},"self":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1074","allowed":["GET"]},"blog":{"ref":"http://connect-dev.amd.com/api/core/v2/blogs/1267","allowed":["GET"]},"members":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1074/members","allowed":["GET"]},"discussions":{"ref":"http://connect-dev.amd.com/api/core/v2/groups/1074/discussions","allowed":["GET","POST"]}},"id":1074,"type":"group","creationDate":"2012-05-07T19:24:31.880+0000","modificationDate":"2012-09-07T02:00:51.821+0000","viewCount":544}

It is actually longer than that

11
  • 2
    Do alert(JSON.Stringify(data)); so you can see the contents of the object, and copy/paste that here for us Commented Sep 25, 2012 at 15:48
  • 1
    @mcpDESIGNS It would make more sense to access xhr.responseText directly, instead of stringifying the object that was created by parsing that very response text. Commented Sep 25, 2012 at 15:50
  • 1
    @ŠimeVidas, I just meant so that we could all see the data in order to help him know how to handle it Commented Sep 25, 2012 at 15:51
  • 1
    It should be stringify, not Stringify. @mcpDESIGNS accidentally capitalized it. Commented Sep 25, 2012 at 15:54
  • 1
    But try @prodigitalson's comment. I think that's the answer. Commented Sep 25, 2012 at 15:57

1 Answer 1

1

If you are outputting stringifying data and you have inside that an array named data then you need to do data.data[0] instead of data[0].

It would be a little more clear for you if you said:

success: function(result, status, xhr){
  var data = result.data;

Then data[0] would work just fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.