2016-03-24 13 views
-1

die retorno json_encode es nichts zurückkehr

ist dies die js Controller ich möchte die json „retorno“

$.ajax({ 
    url: 'controll/busqueda.php', 
    data: { 
     format: 'json', 
     "precio_min": "", 
     "precio_max": "", 
     "rate": "", 
     "asignatura": "" 
    }, 
    success: function(retorno) { 
     alert(retorno.profesores); 
    }, 
    error: function() { 
     alert("aaaaa"); 
    }, 
    type: 'GET' 
}); 

dies ist die PHP-Datei, wo ich zurückkehren das Modell db nennen und $ retorno ist ein stdClass() aus der Datenbank, wo ich in ihnen profesores

if($precio_max != "" || $precio_min != ""){ 
    $retorno = $busqueda->profesor_precio($precio_min,$precio_max); 
}else{ 
    if($rate != ""){ 
     $retorno = $busqueda->profesor_rate($rate); 
    }else{ 
     if($asignatura != ""){ 
      $retorno = $busqueda->profesor_asig($asignatura); 
     }else{ 
      $retorno = $busqueda->profesor(); 
     } 
    } 
} 

echo json_encode($retorno, true); 
+0

Haben Sie die AJAX-Anfrage/-Antwort in den Entwicklertools Ihres Browsers gesehen? –

+0

oh und das Ergebnis von retorno.profesores ist immer undefiniert und es sollte und Array –

+0

Also - es ist nicht Ihre AJAX, es ist Ihre PHP, die nicht funktioniert. –

Antwort

2

1) Ihr Server korrekte Antwort mit Kopf zurückgeben muss: Content-type: application/json . Sie können es mit diesem Code in PHP tun:

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

Sie diesen Header vor jedem Ihrer echo zu schicken.

2) set dataType Eigenschaft für AJAX-Anfrage. Zum Beispiel:

$.ajax({ 
    url: 'controll/busqueda.php', 
    dataType: 'json', 
    data: { 
     param: 'value' 
    }, 
    success: function(retorno) { 
     alert(retorno.profesores); 
    }, 
    type: 'GET' 
}); 

3) stellen Sie sicher, dass Ihre Anfrage controll/busqueda.php kehrt gültige Json-Daten. Überprüfen Sie Ihre Antwort von http://yourhost.name/controll/busqueda.php Gibt es gültige json zurück?

+0

ich tat all das und funktioniert nicht es tatsächlich geben nur Fehler jetzt –

+0

sollte dies mein JSON sein, wenn ich es auf localhost versuchen/.../busqueda.php {"respuesta": 200, "info": "Profesoren Encontrados", "profesores": [{"id_profesor": "1", "nombre": "Guillermo", "email": "[email protected]", "kontra": "memo20", "nacimiento": "1994-10-11", "sexo": "0", "telefono": "424223344", "precio": "2000 "," foto ": null}] –

+0

Sie vermissen'} 'am Ende der Antwort. Ist das nur ein Tippfehler? –