Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
How to retrieve json data from php

i want to retrive json data from php with ajax and output it for test. but it did not work.

Client index.php

<html>
<head>
<title>kakak</title>
</head>
<body>
<p id="demo"></p>

<script>

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = myObj.name;
    }
};
xmlhttp.open("GET", "json.php", true);
xmlhttp.send();

</script>
</body>
</html>

And this's json.php already have object.

<?php

$myArray = ["name" => "john", "age" => 30, "city" => "Japan"];

$myObj = json_encode($myArray);

?>

I want to retrieve object name but it dont ouput anything.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to echo the JSON string in PHP code.

echo $myObj;

Final code would be:

<?php

$myArray = ["name" => "john", "age" => 30, "city" => "Japan"];
$myObj = json_encode($myArray);
echo $myObj;
// or directly
// echo json_encode($myArray);
?>
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda