0
Good night!
I got the following:
var carregarUsuario = function () {
$http.get("buscar.php").then(function (retorno){
console.log(retorno.data);
$scope.usuarios = retorno.data;
});
};
carregarUsuario();
And PHP
error_reporting(0);
$user = "root";
$password = "";
$db = "angulardb";
$host = "localhost";
$con = mysqli_connect("localhost", $user, $password, $db);
if (mysqli_connect_errno()){
echo "Erro: " . mysqli_connect_error();
}
$usuario = mysqli_query($con, "SELECT * FROM users");
header('Content-Type: application/json');
$return = array();
while ($dados = mysqli_fetch_assoc($usuario)) {
array_push($return, $dados);
}
echo json_encode($return);
No data is being displayed on the page, or console. What may be?
EDIT
When I put a print_r
in my PHP code, right after the while
I have:
Array
(
[0] => Array
(
[0] => 2
[id] => 2
[1] => João Silva
[nome] => João Silva
[2] => [email protected]
[email] => [email protected]
[3] => 123456
[pass] => 123456
)
[1] => Array
(
[0] => 3
[id] => 3
[1] => Mario de Almeida
[nome] => Mario de Almeida
[2] => [email protected]
[email] => [email protected]
[3] => 123456
[pass] => 123456
)
)
Look at your console’s returned some error.
– Vinicius Silva
Your console shows absolutely nothing? What if you only use
console.log(retorno)
?– celsomtrindade
@Viniciussilva the island is blank
– Lucas Torres
@Using return only, the following is displayed: Object {date: ", status: 200, config: Object, statusText: "OK"}
– Lucas Torres
@Lucastorres then make sure that php is processing the data correctly, use some 'print_r' to see what the returns are. Could be a problem in php in data generation.
– celsomtrindade
@Celsomtrindade, after the push array_push, still inside the while block, I added print_r($Return); and it returns me the data: Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => João Silva [name] => João Silva [2] => [email protected] [email] => [email protected] [3] => 123456 [pass] => 123456 ) Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => João Silva [name] => João Silva [2] => [email protected] [email] => [email protected] [3] => 123456 [pass] => 123456 ) [1] => Array ( [0] => 3 [id] =>...etc
– Lucas Torres
1- Open your Chrome, 2 - enable developer tools 3 - Go to the networking tab. 4, Look what’s coming from Answer in your php, see if it’s a valid JSON or if it’s coming some Error 500, etc... Basically your problem is in the return of AJAX. I suggest creating a
catch
of error:$http.get("buscar.php").then(funcao sucesso, funcao catcherro)
– Roger Barretto