user2406735
user2406735

Reputation: 245

jQuery - access multidimesional array from ajax response

I am sending some data with ajax, and as a response i get multidimensional array.

        $.ajax({
            type: "POST",
            url: "/slideshow/list.php",
            data: imageId,
            success: function(data){
                imagesList = data;
                console.log(imagesList);
                curentImage = imagesList[0];
            }
        });

Response, data looks like this. This is what i get on console.log(imagesList): I am using php, and the response is provided like this <?php echo json_encode($data) ?>

[
   [1,487124,"<img src=\"http:\/\/example.com\/images\/1\/487124.jpg\" \/>","http:\/\/example.com\/photos\/salle-a-manger---mineral\/649518","Title 1"],
   [2,732924,"<img src=\"http:\/\/example.com\/images\/1\/732924.jpg\"\/>","http:\/\/example.com\/photos\/salle-a-manger---","Title 2"],
   [3,341649,"<img src=\"http:\/\/example.com\/images\/2\/341649.jpg\"\/>","http:\/\/example.com\/photos\/salle-a-manger---","Title 3"]
]

If i try to access the first array with imagesList[0] it only shows [

How can i access the first or second array, or values inside of them?

Upvotes: 0

Views: 66

Answers (2)

iubema
iubema

Reputation: 349

Use this

var obj = $.parseJSON(data);

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

specify the dataType in ajax request

 $.ajax({
            type: "POST",
            url: "/slideshow/list.php",
            data: imageId,
            dataType:"json",
            success: function(data){
                 $.each(data,function(key,value){
                       console.log('key:'+key+", value:"+value);
                       //do your stuff 
                 });
            }
        });

Upvotes: 1

Related Questions