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

85
Vistas
PHP output not sliceable by Javascript

I am using the following array in Javascript and it works perfectly when I use the slice command to access variables

dataSequence = [
    {
        name: 'Week 1',
        data: [1, 2, 2, 1, 1, 2, 2,5]
    }, {
        name: 'Week 2',
        data: [6, 12, 2, 3, 3, 2, 2,6]
    }
    ];

Then when I use this PHP script via Ajax to show the same data.

$out = "[
    {
        name: 'Week 1',
        data: [1, 2, 2, 1, 1, 2, 2,5]
    }, {
        name: 'Week 2',
        data: [6, 12, 2, 3, 3, 2, 2,6]
    }
    ]";
 echo $out;

here is my Ajax call

$.ajax({
url: "gettimedata1.php",
type: 'POST',
}).done(function(msg) {
dataSequence = msg;
data = dataSequence[0].data.slice();
});

It throws the message "Cannot read property 'slice' of undefined".

What do I need to do to format the PHP output correctly ?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe this is the correct way to output json from PHP

$a = array(
        array('name' => 'week1', 'data' => [1, 2, 2, 1, 1, 2, 2, 5]),
        array('name' => 'week2', 'data' => [6, 12, 2, 3, 3, 2, 2, 6])
);
$json = json_encode($a);
header('Content-Type: application/json');
echo $json;

You should then be able to:

$.ajax({
  url: "gettimedata1.php",
  type: 'POST'
}).done(function(response) {
  data = response[0].data.slice();
});

You may have to fiddle with the PHP a little I do not have a PHP environment to test on and its been a few years since I last coded in it.

about 4 years ago · Juan Pablo Isaza 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