How to get javascript/jquery ajax array values

Asked

Viewed 976 times

0

I am making an ajax request and returning the data by php with json_encode

server side:

 $id=1;
 if(isset($_POST['id'])){
   $id = $_POST['id']; 
 }else{
   http_response_code(400);
 }
 $produto = listaAssociada($id);
 echo json_encode(listaAssociada($id));

function listed

  $query = "SELECT p.nome,p.descricao as produtodescricao,p.img as produtoimg,e.titulo,e.descricao as explicacaodescricao,e.infoadicional,e.img as explicacaoimg FROM Produto as p inner join Explicacao as e on p.idExplicacao=e.idExplicacao where p.idProduto= $id"; 
$consulta = mysqli_query($mysqli, $query);

    if(mysqli_num_rows($consulta) != 0) {
        while($item = mysqli_fetch_array($consulta)) {
            $resultados['nome'] = $item['nome'];
            $resultados['produtodescricao'] = $item['produtodescricao'];
            $resultados['explicacaodescricao'] = $item['explicacaodescricao'];
            $resultados['titulo'] = $item['titulo'];

        }

        return $resultados;
    }else{
        return 0;
    }

part of the script

<script type="text/javascript">
$(document).ready(function(){
  var produtos;
  var i=0;
  $.ajax({
    type: "POST",
    url: "scripts/produto.php",
    data:{
      id:1
    },
    error:function(e){

    },

    success: function(data){
        alert(data);
    }
  });
 }); 

I’ve tried json.parse doesn’t work, I’ve tried foreach too. I want to be able to take the separate values to do for example

   var x = data.nome;

when I give date Alert returns. inserir a descrição da imagem aqui

  • Tries putting dataType:"json", in Ajax

  • when I put dataType:"json", it stopped working, until a simple Alert('hi'); it was giving these two messages on the console: GET http:/localhost/favicon.ico 404 (Not Found) product.php:1 Unchecked Runtime.lastError: The message port closed before a Response was Received.

  • There’s something wrong. You should return a JSON like this: {nome: "nome do item", produtodescricao: "descricao do produto", explicacaodescricao: "explicacao do produto", titulo: "titulo"}explicacaodescricao: "explicacao do produto"nome: "nome do item"produtodescricao: "descricao do produto"titulo: "titulo"}

  • psé I don’t know if maybe it’s not the way I’m storing in the vector on the php side. but in the script itself I can access as a direct array, I make $product = listAssociated($id); echo $product['name'] . " <br>". and printa straight.

  • What is the jQuery version you are using?

  • last version 3.4.1 .

Show 1 more comment

2 answers

0

On the server side, start with:

header('Cache-Control: no-cache, must-revalidate'); 
header('Content-Type: application/json; charset=utf-8');

This before the $id=1;, and in the script, instead of using Alert, try using console.log(data);.

  • giving a console.log it appears the same thing as in Alert, and I can’t access it with date['name'] for example is said to be undefined.

  • ai tries date.name, date.productdescription, date.explicacaodescription, ... Now it is a json, so just put point "." and the name of what you’ll want.

  • also doesn’t work.

  • And if you just put date, what appears Gabriella?

  • what I showed in the picture

  • but on the.log console as well?

  • yes same thing =(

  • Look, I don’t know if it helps much, but I only left two files, the product.php and index.html, follow them:

  • https://drive.google.com/file/d/138DhHiswTrbyoyMiSm_x1M3QWHlhGbxr/view?usp=sharing

  • psé didn’t work much, but thanks for the help.

Show 5 more comments

0

The return shown in Alert indicates that it is an array and not an object. This way, you must access using the index name:

 var x = data['nome'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.