我读了很多以前的问题,但我无法解决。我真的很感激帮助。 我正在使用xml的jqGrid,它工作得很好,现在我需要使用JSON,但网格总是空的。我正在和WAMP合作。
服务器端回答此问题:
{
"page":"1",
"total":"1",
"records":"2",
"rows":[
{"id":"campo1","cell":["campo1","campo3"]},
{"id":"campo11","cell":["campo11","campo33"]}
]
}
第一个HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/json; charset=ISO-8859-1"/>
<title>jqGrid Ejemplo 1: Cargar datos de una tabla MySql </title>
<link rel="stylesheet" type="text/css" media="screen" href="css/flick/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="jqgrid/css/ui.jqgrid.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<script src="jqgrid/js/i18n/grid.locale-es.js" type="text/javascript"></script>
<script src="jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
jQuery("#tblclientes").jqGrid({
url:'clientes2.php',
datatype: 'json',
mtype: 'POST',
colNames:['ID','NOMBRE'],
colModel:[
{name:'idCliente', index:'idCliente', width:50 },
{name:'nombre', index:'nombre', width:160 }
],
pager: '#paginacion',
rowNum:10,
sortname: 'idCliente',
sortorder: 'asc',
viewrecords: true
});
});
</script>
</head>
<body>
<table id="tblclientes"></table>
<div id="paginacion"> </div>
</body>
</html>
服务器端
<?php
$page = $_POST['page'];
$limit = $_POST['rows'];
$sidx = $_POST['sidx'];
$sord = $_POST['sord'];
if(!$sidx) $sidx =1;
$conexion = new mysqli("127.0.0.1","root","","deyertdb");
$result = $conexion->query("SELECT COUNT(*) AS count FROM code2");
$fila = $result->fetch_array();
$count = $fila['count'];
if( $count >0 ) $total_pages = ceil($count/$limit);else $total_pages = 0;
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
$consulta = "SELECT StockCode as idCliente, Mfr as nombre FROM code2 ORDER BY $sidx $sord LIMIT $start , $limit;";
$result = $conexion->query($consulta);
$respuesta->page = $page;
$respuesta->total = "$total_pages";
$respuesta->records = $count;
$i=0;
while( $fila = $result->fetch_assoc() ) {
$respuesta->rows[$i]['id']=$fila["idCliente"];
$respuesta->rows[$i]['cell']=array($fila["idCliente"],$fila["nombre"]);
$i++;
}
echo json_encode($respuesta);
?>
答案 0 :(得分:0)
我无法重现您描述的问题。 The demo几乎使用您的代码(只需很少修改)并使用我保存在文件中的JSON数据。一切都没有任何问题。我不是PHP开发人员,但很可能你应该添加行
header("Content-Type: application/json; charset=utf-8");
之前echo json_encode($respuesta);
答案 1 :(得分:0)
$response = new stdClass();
之前
$response->page = $page;
感谢Oleg和Mariusz
最后一部分的详细解决方案 here