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]);
        }
    });

的输出data

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

但是当我尝试输出data[0]data[1]它们都以undefined. 我不确定如何处理这个 JSON 对象。

的输出alert(data)

[Object object]

所以我假设它是一个格式正确的 JSON 对象。

这是输出 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":"david.kyle@amd.com","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":"kristi.fontenot@amd.com","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}

它实际上比那更长

4

1 回答 1

1

如果您要输出字符串化数据,并且其中有一个名为 data 的数组,那么您需要执行 data.data[0] 而不是 data[0]。

如果您说:

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

然后 data[0] 就可以了。

于 2013-07-09T18:27:24.337 回答