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[0] の代わりに data.data[0] を実行する必要があります。

次のように言うと、もう少し明確になります。

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

その後、 data[0] は問題なく機能します。

于 2013-07-09T18:27:24.337 に答える