2011-03-17 107 views
1

嗨:直指點。
我試着從一個Ajax調用傳遞一個JSON字符串作爲數據源 PHP代碼JSON數據表與JSON字符串

if($_POST){ 
    $action = $_POST["action"]; 
    if($action == "call_data"){ 
     header('Content-type: application/json'); 
     include_once 'clases/Usuario.class.php'; 
     $usuario = new Usuario; 
     $resultado = $usuario->listar_jefes(); 
     if (! $resultado) 
     { 
      exit ("nok"); 
     } 
     exit (json_encode ($resultado)); 
    } 
} 

我的jQuery代碼。

$.post("function.php", {action:"call_data"},function(jsonstr){ 
    $("#usr_table").dataTable({ 
     "bProcessing": true, 
     "sAjaxSource": jsonstr 
    }); 
}); 

但它不工作...任何幫助,將不勝感激

編輯:我把我的表...以防萬一:

<table id="usr_table"> 
    <thead> 
     <tr> 
      <th>col1</th> 
      <th>col2</th> 
      <th>col3</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 

     </tr> 
    </tbody> 
    <tfoot> 
     <tr> 
      <th>col1</th> 
      <th>col2</th> 
      <th>col3</th> 
     </tr> 
    </tfoot> 
</table> 
+0

您是否收到回覆?另外,您在回調中接受jsonstr的參數。你期待這是一個字符串還是一個對象? – Jage 2011-03-17 15:11:36

+0

是@Jage,我有一個響應...一個json編碼的字符串。 [code]「[{\」usuario_id \「:\」37 \「,\」run \「:\」100428725 \「,... [/ code] – JuanToroMarty 2011-03-17 15:31:14

+0

聽起來像你的dataTable沒有做正確的事情。你確定它不期待一個JSON對象,並且你正在傳遞一個JSON字符串? – Jage 2011-03-17 15:33:41

回答

0

你得到的數據回來了嗎?如果是這樣,你可能需要將你獲得的數據轉換回json對象。

var json_object = eval('(' + returned_data_string + ')'); 
+0

是的,我正在接收數據。一個JSON編碼的字符串。我試圖用json2.js JSON.parse(jsonstr)函數解析它,但它沒有幫助。 – JuanToroMarty 2011-03-17 15:38:03

+0

eval是邪惡的!請避免。 – Scott 2012-12-23 22:09:58