0

您好,我有一个 JQuery/Ajax 函数,如下所示:

$.ajax({
    type : "POST",
    url  : "/posts/getids", 
    success: function(response){
        console.log(response);                                                                  
    },
    error: function() {
        alert('An unexpected error has occurred! Please try later.');
    }
});

在我的 cakePHP 脚本中,我使用 json_encode($array) 函数发送一个数组。

在萤火虫我得到这个结果:

[{"Post":{"id":1}},{"Post":{"id":2}},{"Post":{"id":4}},{"Post":{"id":3}}]

所以我的问题是我怎样才能简单地打印这样的ID:1、2、3、4

谢谢你。

4

1 回答 1

1
// Convert JSON to JavaScript array.
var dataFromServer = JSON.parse(response);

// Creating an array of id's.
var idArray = [];

// Moving data to "idArray".
for(i = 0; i < dataFromServer.length; i++){
    idArray[i] = dataFromServer[i].Post.id;
}

// Checking the result.
console.log(idArray);

// [1, 2, 3, 4].
于 2013-03-12T17:45:39.627 回答