2011-03-17 108 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