Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda