如果是自定义数组的请加 [ ] 不然会报错:
Uncaught TypeError: Cannot read property 'length' of null
Uncaught TypeError: Cannot read property 'length' of undefined
例子代码:
前台代码: user.hmtl
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- 引入easyUI1.43 -->
<script type="text/javascript" src="__PUBLIC__/JS/jquery-easyui-1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/JS/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/JS/jquery-easyui-1.4.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="__PUBLIC__/JS/jquery-easyui-1.4.3/themes/icon.css">
<script type="text/javascript" src="__PUBLIC__/JS/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js" charset="uft-8">
</script>
<script type="text/javascript" charset="UTF-8">
$(function(){
$('#datagrid').datagrid({
url:'__URL__/userList',
columns:[[
{field:'id',title:'id',width:100},
{field:'name',title:'Name',width:100},
{field:'pwd',title:'pwd',width:100,align:'right'}
]]
});
});
</script>
</head>
<body>
<table id="datagrid">
</table>
</body>
</html>后台:数组示列:
public function userList(){
$arr = array(
'id' => '1',
'name' => '李三',
'pwd' => 'abc',
);
$json_string = json_encode($arr);
$data='['.$json_string.']';
$obj = json_decode($data);
$this->ajaxReturn($obj);
}数据库:public function userList(){
$User = M("User");
$arr = $User->select();
$json_string = json_encode($arr);
$obj = json_decode($json_string);
$this->ajaxReturn($obj);
} 最佳答案