Receive and read array from Ajax

-1

I have a request ajax so

$.ajax({
    type: "POST",
    data: "valor="+val,
    url: "ajax/mostra_imagens.php",
    success: function(e){
        console.log(e);
    }
});

On page mostra_imagens.php it creates a array which in turn is received in the variable e but how do I make the variable e be a array and not a string ?

show_pictures.php

<?php
require "../../ligacao.php";
$array = array();
$query=mysqli_query($db, "SELECT * FROM publicidades_sponsors where id_publicidade = '".$_POST['valor']."'");
while($mostra=mysqli_fetch_assoc($query)){
    $array[] = $mostra['nome'];
}
print_r($array);
?>

What console.log(e) returns

Array
(
    [0] => DIDAXIS1.png
    [1] => DIDAXIS2.png
    [2] => DIDAXIS3.png
    [3] => DIDAXIS4.png
)

It returns different data depending on what is selected in select

    
asked by anonymous 18.06.2018 / 12:54

4 answers

0

Well, I got somehow what I wanted to do.

while($mostra=mysqli_fetch_assoc($query)){
    $array[] = $mostra['nome'];
}
foreach($array as $value){
    echo $value.",";
}

With this code it creates a comma-separated string that I can use later.

success: function(e){
    var slice = e.slice(0,-1);
    expl = slice.split(",");
        for(t=0;t<=balance;t++){
            if(n_pub-1<=t){
                r=0;
            }
            console.log("???"+expl[r]);
            $(".images"+p+" .images_pub").html("<img src='pubs/"+valor+"/"+expl[r]+"' style='width:100%' /><div class='altera' style='margin-top:-20px;top:0;left:2px;position:absolute;color:white;'><small><small><small><b>"+pub+"</b></small></small></small></div>");
            p++;
            r++;
        }
}

With this I get what I need. Using slice(0, -1) I remove the last comma to not conflict and create another position in array created with split(",") .

    
19.06.2018 / 11:20
0

I do not know exactly how your array is coming, the more you sweep an array you can use jQuery.each ()

var array = [3, 2, 3, 4];
$.each(array, function(index, val) { 
  console.log("Indice: " + index + " Valor: " + val)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
18.06.2018 / 14:50
0

You have a few options.

You can use the dataType option of jQuery, so it tries to guess the response type of the server depending on the MIME sent in it, if you do not control it or you prefer not to change, you can put this value as json to and jQuery will transform the response into a javascript object (if possible).

$.ajax({
    // ... outras opcoes
    dataType: 'json'
});

You can also do it in success conversion, without using dataType above.

$.ajax({
    success: function(e) {
        console.log(JSON.parse(e));
    }
});

EDIT

With the new details placed in the question, what is above is not valid. But it remains a viable option when the API returns the correct JSON, but jQuery does not interpret it as such.

    
18.06.2018 / 15:00
0

Do not show_imagens.php, you can do this instead of print_r() of your example,

<?php
    require "../../ligacao.php";

    $array = array();
    $query = mysqli_query($db, "SELECT * FROM publicidades_sponsors where id_publicidade = '{$_POST['valor']}'");
    while ($mostra = mysqli_fetch_assoc($query)) {
        $array[] = $mostra['nome'];
    }

    // Troca o print_r() pela linha de baixo.
    echo json_encode($array);
?>
    
19.06.2018 / 13:52