2016-07-11 6 views
2

Ich versuche, Ajax Antwort in json zurückzukehren, aber wenn ich es in Protokoll ausdrucken gibt es null sogar Tabellen haben Zeilen, mein PHP-Code ist:Rückkehr Tabellen Zeilen in json und Druck

if(isset($_GET['proid'])){ 
    $projid = $_GET['proid']; 
    include(db.php); 
    $res = mysqli_query($con, "SELECT * FROM data WHERE project_id LIKE '%$projid%'");  
    while($row = mysqli_fetch_assoc($res)) 
    { 
     $dataarray[] = $row; 
    } 
    echo json_encode($dataarray); 
} 

Ajax:

$.ajax({ 
     url : 'getRecStudy.php', 
     type : 'GET',   
     data : {proid:study}, 
     success : function(data) { 
      $('#tbody').empty(); 
      $("#tbody").append(data); 
      console.log(data); 
     } 
    }); 

was ist falsch?

+0

Was sind Ihre Konsolendaten? –

+0

Versuchen Sie die (json_encode ($ dataarray)); – rad11

+0

Kann man 'echo json_last_error();' – Will

Antwort

0

Verwendung parseJSON Methode in Erfolgsfunktion wie diese

var obj = jQuery.parseJSON(data); 

alert(obj.name); // For example name is a key 
+0

auch wenn wir nicht analysieren, würde es noch einige JSON-codierte Daten nicht "Null" -Wert protokollieren. Sein loggin "Null" -Wert, so dass null JSON analysiert, wird nicht korrekt geparsten Wert erzeugen. –

1

finde ich kein Problem in Ihrem Code außer varibales. Sie müssen den Code in PHP-Datei

if(isset($_GET['proid'])){ 
    echo $_GET['proid'] . " is proid"; 
    $projid = $_GET['proid']; 
    include(db.php); 
    echo "db connected"; 
    $res = mysqli_query($con, "SELECT * FROM data WHERE project_id LIKE '%$projid%'");  
    echo "result fetched"; 
    while($row = mysqli_fetch_assoc($res)) 
    { 
     $dataarray[] = $row; 
     echo "inside while"; 
    } 
    echo json_encode($dataarray); 
    print_r($dataarray); 
    exit; 
} 

danach debuggen alle http://yourdomain.com/yourfile.php?proid=correctvalue Hit

Sie den Bug bekommen.

+0

können Sie mir ein Beispiel für einen Link von Ajax Json Beispiel geben? – JohnB

+0

http://stackoverflow.com/questions/8649621/returning-a-json-object-from-php-in-ajax-call – Jagadeesh

+0

es gibt immer noch null @ kishore – JohnB