【发布时间】:2011-03-17 15:04:52
【问题描述】:
嗨:直截了当。
我试图从 ajax 调用传递一个 JSON 字符串作为数据源
PHP代码
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>
【问题讨论】:
-
您收到回复了吗?此外,您在回调中接受参数 jsonstr。你希望它是一个字符串还是一个对象?
-
是的@Jage,我有一个响应......一个json编码的字符串。 [code]"[{\"usuario_id\":\"37\",\"run\":\"100428725\",...[/code]
-
听起来你的 dataTable 没有做正确的事情。你确定它不期望一个 JSON 对象,并且你正在传递一个 JSON 字符串吗?
-
我尝试用 json2.js JSON.parse(jsonstr) 函数解析它,但也不起作用
-
这是否意味着它期待一个对象呢?如果您要求,Jquery 会自动将其作为对象返回给您( [code]$.post(url, data, callback, 'json');[/code] )。关键是,该过程在 dataTable() 中失败。该代码以及您返回的对象的完整示例将更有助于诊断您的问题。
标签: php jquery jquery-plugins datatable