从php返回的json无法解析为jQuery dataTables [英] json returned from php cannot be parsed for jQuery dataTables

查看:164
本文介绍了从php返回的json无法解析为jQuery dataTables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有库书的简单mysql数据库表.我正在使用php页面来检索书籍列表.这是它返回的内容:

I have a simple mysql database table with library books. I am using a php page to retrieve the list of books. This is what it returns:

php get_books.php

php get_books.php

{"iTotalRecords":"1","aaData":[{"author":"Tim Powers","title":"The Anubis Gates","genre":"Fiction","publisher":null,"year":null,"location":"Bookshelf","notes":null}]}

在jQuery dataTables中,我有:

In jQuery dataTables, I have:

<script >
    $(document).ready(function() {
    $('#books').DataTable({
        "bServerSide": true,
        "sAjaxSource": "./get_books.php"
    });
});
</script>

当我使用该脚本运行网页时,会收到警报:

When I run the web page with that script I get an alert:

DataTables警告(表ID ='books'):DataTables警告:无法解析来自服务器的JSON数据.这是由JSON格式错误引起的.

DataTables warning (table id = 'books'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

我找不到格式错误.数据应如何格式化.

I can't find what the formatting error is. How should the data be formatted.

这是返回JSON数据的php页面:

Here is the php page that does the return of the JSON data:

<?php
    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;
    $result = array();

    include 'conn.php';

    $rs = mysql_query("select count(*) from books");
    $row = mysql_fetch_row($rs);
    $result["iTotalRecords"] = $row[0];
    $rs = mysql_query("select * from books limit $offset,$rows");

    $items = array();
    while($row = mysql_fetch_object($rs)){
        array_push($items, $row);
    }
    $result["aaData"] = $items;

    echo json_encode($result);

?>

退货应该是什么样子,我该如何生产?

What should the return look like and how do I produce it?

推荐答案

发现了问题,这对我来说很愚蠢! 在OS X El Capitan(10.11.2)上,Safari或任何其他浏览器均未识别php文件,因为它们位于我的主目录中,而不位于apache的/Library/WebServer/Documents根目录中!

Found the problem and it was stupid on my part! On OS X El Capitan ( 10.11.2 ), php files were NOT getting recognized by Safari or any other browser because they were in my home directory and not in the /Library/WebServer/Documents root for apache!

我将项目移到该目录中,因为无法像El Capitan之前那样设置要使用用户目录〜/Sites的东西(以后会再做更多工作).

I moved my project into that directory as I could not get things set up to use my user directory ~/Sites as you could before El Capitan ( will work on that some more in the future ).

进行此更改后,由于执行了包含在ajax参数中的php文档,因此一切正常!

With that change, the php documents get executed as they are included in the ajax argument and all works fine!

感谢大家的帮助.很抱歉浪费了时间,但是我的test.php可以运行,但是我没有注意到它在Web服务器的根目录中!

Thanks every one for your help. Sorry to have wasted time, but my test.php worked, but I hadn't noticed that it was in the web server root directory!

这篇关于从php返回的json无法解析为jQuery dataTables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆