2012-11-17 38 views
0

有人能帮助我并指出我错在哪里。感谢你的帮助。网格显示空白,没有任何东西正在加载。JqGrid无法从PHP的JSON格式加载数据

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script> 
    <script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.1.1/js/i18n/grid.locale-en.js"></script> 

<script type="text/javascript"> 
     $.jgrid.no_legacy_api = true; 
     $.jgrid.useJSON = true; 
</script> 

<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.1.1/js/jquery.jqGrid.src.js"></script> 

<script type="text/javascript"> 

jQuery(document).ready(function(){ 

jQuery("#list2").jqGrid({ 
    url:'http://localhost/jqgrid/book.php', 
    datatype: "json", 
    colNames:['Title', 'Author', 'Publisher'], 
    colModel:[ 
     {name:'title' , width:160}, 
     {name:'author' ,width:180}, 
     {name:'publisher' ,width:80} 
    ], 
    rowNum:10, 
    rowList:[10,20,30], 
    pager: '#pager2', 
    sortname: 'title', 
    sortorder: "desc", 
    height: 'auto', 
    width: 900, 
    viewrecords: true, 
    gridview: true, 
    caption:"JSON Example", 
    jsonReader : { 
     repeatitems: true, 
     cell: "cell", 
     id:"id", 
     userdata: "userdata", 
     root: "rows", 
     page: "page", 
     total: "total", 
     records: "records" 
     } 
}); 

jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:true,del:true}); 

}); 

</script> 

</head> 

<body> 

    <table id="list2"><tr><td></td></tr></table> 
    <div id="pager2"></div> 

</body> 
</html> 

这里从PHP我的JSON响应:

{ “页”: “1”, “总”:1, “记录”: “2”, “行”:[{ “ID” :“2”,“cell”:[“PHP QUERY”,“JSON”,“VTECH”]},{“id”:“1”,“cell”:[“JQUERY Cook book”,“MY AUTHOR”, “GRID”]}]}

回答

0

您发布的代码通常是正确的。你可以在the demo上看到。

问题的原因可能是,例如绝对URL路径的相对像url:'book.php'url:'/jqgrid/book.php'url:'http://localhost/jqgrid/book.php'代替使用。另一个可能的原因可能是JSON响应的HTTP头中的值Content-Type错误。我建议你绝对不要在Ajax请求中使用绝对URL,因为你只能使用来自的URL,因为same origin policy是同一个web服务器。所以最好总是使用相对的URL。