Error when executing in localhost

0

I'm trying to make a mini application in angular but I get an error when executing it.

My controller:

var app = angular.module("pacientesApp",[]);
//controller
app.controller("controladorPacientes", function controllerPacientes($scope, $http){
    $http.get("../pacientesJSON.php").success(function(r){
        $scope.pacientes = r.pacientes;
    });
});

This calls a php:

<?php
$usuario ="root";
$pwd = "";
$servidor = "localhost";
$db = "loquesea";
$link = mysql_pconnect($servidor, $usuario, $pwd);

if(!$link){
    header("location:error_mysql.html"); exit;
}

if(!mysql_select_db($db)) {
    header("location:error_mysql.html"); exit;
}

//buscar todos los registros
$query = "SELECT * FROM paciente ORDER BY ID";
//se guarda el resultado de la query
$resultado = mysql_query($query)
or die ("Error FATAL al buscar datos de $db: " .mysql_error());
$i = 0;
$registros = mysql_num_rows($resultado);

print '{"pacientes": [';
while( $renglon = mysql_fetch_object($resultado)){
    print "{";
    $nombre = $renglon->nombre;
    $apellidos = $renglon->apellidos;
    $edad = $renglon->edad;
    $domicilio = $renglon->domicilio;
    $telefono = $renglon->telefono;
    $email = $renglon->email;
    $dni = $renglon->dni;
    print '"nombre":"'.$nombre.'",';
    print '"apellidos":"'.$apellidos.'",';
    print '"edad":"'.$edad.'",';
    print '"domicilio":"'.$domicilio.'",';
    print '"telefono":"'.$telefono.'",';
    print '"email":"'.$email.'",';
    print '"dni":"'.$dni.'",';
    print "}";
    $i++;
    if($i<$registros){

        print ",";
    }
}
print "]}";
mysql_close($link);
?>

and inside my html I call my controller:

<body>
    <div ng-app="pacientesApp" ng-controller="controladorPacientes">
        <ul>
            <li ng-repeat="p in pacientes">
                {{p.nombre+", "}}
            </li>
        </ul>
    </div>
</body>

The question is that when I open it in localhost it does not show me anything, and if I see it in the console it sends me the following error:

I can not find the fault.

    
asked by alex 25.09.2016 в 15:26
source

2 answers

1

The problem is clearly the JSON that is malformed. It is not necessary nor a good practice to do this kind of thing manually when php already includes a well proven mechanism to do it.

Using json_encode , but requires a bit of refactoring of the code:

// creamos una lista
$lista = array();

while( $renglon = mysql_fetch_object($resultado)){
    // creamos un registro.
    $r = new stdClass();
    $r->nombre = $renglon->nombre;
    $r->apellidos = $renglon->apellidos;
    $r->edad = $renglon->edad;
    $r->domicilio = $renglon->domicilio;
    $r->telefono = $renglon->telefono;
    $r->email = $renglon->email;
    $r->dni = $renglon->dni;

    // lo agregamos a la lista.
    $lista[] = $r; 
}

// luego generamos un objeto con la pro. pacientes
$response = new stdClass();
$response->pacientes = $lista;

$json = json_encode($response);
    
answered by 25.09.2016 в 15:45
0
  

I can not find the fault

The failure is probably that you do not return a valid JSON from your backend. The first thing you should do is make sure that you return the expected value. It is very important to add to our habits to carry out tests periodically. You can do it with a simple dump or through a library of unit tests.

That said, there are 2 errors in your code:

  • Do not set the header for JSON
  • You do not send the JSON using json_encode

Your code is not readable, do not expect to understand it in a few months. The conventional way to send a JSON from PHP is using json_encode :

header('Content-Type: application/json');

$json = array();
while($row = mysql_fetch_assoc($resultado)) {
  $json[] = $row;
}
echo $json_encode($tu_json);
    
answered by 25.09.2016 в 15:43