0
I have the following ajax.
var ajax = new XMLHttpRequest();
ajax.open("GET", 'http://localhost/erp/api/v1/itens.php');
ajax.responseType = "json";
ajax.send();
ajax.addEventListener("readystatechange" , function () {
if (ajax.readyState === 4 && ajax.status === 200){
var response = ajax.response;
console.log(response.length);
}
})
However the Console.log(reponse.length) brings me 'Undefined', and if I print the reponse brings me 6 data, the PHP that brings the data is this.
$i = 1;
while($lista = $this->ListarDados()):
$produtos[$i] = array(
'idProduto'=> $lista['id_Produto'],
'nomeProduto' => $lista['nomeProduto'],
'referencia' => $lista['referencia'],
'custo' => str_replace(".", ",", $lista['custo']),
'venda' => str_replace(".", ",", $lista['venda']),
'lucro' => str_replace(".", ",", $lista['lucro'])
);
$i++;
endwhile;
echo json_encode($produtos);
That’s the php answer
{"1":{"idProduto":"20","nomeProduto":"teste","referencia":"as2","custo":"10,00","venda":"30,00","lucro":"20,00"},"2":{"idProduto":"21","nomeProduto":"Atacado do Body","referencia":"222","custo":"11,00","venda":"26,55","lucro":"15,55"}}
Someone can help me?
It is necessary to show what is returned from your PHP code. As we cannot as JSON is being formed or returned, it is difficult to give an answer. Please [Edit] your question by adding a [mcve]
– Wallace Maxters
Edited by @Wallacemaxters
– Ricardo
I answered the question. The problem is in the indexes of your array.
– Wallace Maxters