繁体   English   中英

DataTables Symfony中无效的JSON响应

[英]Invalid JSON response in DataTables Symfony

我正在尝试从模板表中的响应中检索数据(使用DataTables),但它引发以下错误:

DataTables警告:表id = example-无效的JSON响应。 有关此错误的更多信息,请参见http://datatables.net/tn/1

这是我用来呈现JSON的代码;

$articleId = $request->getParameter( 'id' );
$data = BlogArticleLinkedArticlePeer::getAllArticles( $articleId );

$response = array( 'data' => $data );

return $this->renderText( json_encode( $response ) );

var_dump( json_encode( $response ) ); 返回以下内容:

string(477) "{
    "data": [
        {
            "id": "1",
            "title": "This is a test article"
        },
        {
            "id": "2",
            "title": "Test header link"
        },
        {
            "id": "4",
            "title": "Adrenal Fatigue"
        },
        {
            "id": "5",
            "title": "Adrenal Fatigue"
        },
        {
            "id": "6",
            "title": "Adrenal Rev 001"
        },
        {
            "id": "7",
            "title": "Adrenal Fatigue test"
        },
        {
            "id": "8",
            "title": "recipe1"
        },
        {
            "id": "9",
            "title": "Recipe2"
        },
        {
            "id": "10",
            "title": "Peanut Butter/Chocolate Fat Bomb"
        },
        {
            "id": "11",
            "title": "Adrenal Fatigue"
        },
        {
            "id": "12",
            "title": "Test"
        },
        {
            "id": "13",
            "title": "okg"
        },
        {
            "id": "14",
            "title": "Let"
        }
    ]
}"

还有var_dump( $this->renderText( json_encode( $response ) ) ); 返回string(4) "None"

以下是我脚本的内容;

<script type="text/javascript">
        $(document).ready(function() {
            $('#articleList').DataTable( {
                "ajax": $(".datatable-cn").data('url'),
                "aoColumns": [
                    { "mData": "id" },
                    { "mData": "title" },
                ],
                "aoColumnDefs": [
                    {"aTargets": [0], "mData": 0, "bSortable": false, "bSearchable": false,
                        "mRender": function(data, type, full) {
                            return '<input class="check-select" type="checkbox" value="' + data + '">'
                        }
                    },
                    {"aTargets": [1], "mData": 1, "bSortable": true, "sClass": "linked"},
                ]
            });
        });
</script>

这里是页面的HTML;

 <div class="pagein" align="center">
        <div align="center">
            <h3>Add Articles</h3>
        </div>
        <div class="datatable-cn" data-url=<?php echo url_for('blog/linkarticle?id='.$articleId); ?>>
            <table id="articleList" class="display" width="100%">
                <thead>
                <th>Select</th>
                <th align="left">Title</th>
                </thead>
            </table>
        </div>
        <br/><br/>
        <div class="fright">
            <input type="button" name="add" id="submit_btn" value="Submit">
            <input type="hidden" name="addedArticle" id="addedArticle" value="">
            <input type="hidden" name="articleId" id="articleId" value="<?php echo $articleId; ?>">
        </div>
        <br/><br/>
    </div>

和我的链接文章功能;

public function executeLinkarticle( sfWebRequest $request ) {
    $this->setLayout( 'popupLayout' );
    $this->articleId = $request->getParameter( 'id' );
    if ( $request->isMethod( 'POST' ) ) {
        $articles = $request->getParameter( 'articles' );
        $articleId = $request->getParameter( 'articleId' );
        foreach ( $articles as $linkedArticleId ) {
            $linkedArticleObj =  new BlogArticleLinkedArticle();
            $linkedArticleObj->setArticleId( $articleId );
            $linkedArticleObj->setLinkedArticleId( $linkedArticleId );
            $linkedArticleObj->save();
        }
        return $this->renderText();
    }
}

我在做什么错,我该如何解决?

您收到的错误表明您返回的JSON无效。

请返回json_encode( $response )而不是$this->renderText( json_encode( $response ) )来解决您的难题,如以下代码段所示:

$articleId = $request->getParameter( 'id' );
$data = BlogArticleLinkedArticlePeer::getAllArticles( $articleId );

$response = array( 'data' => $data );

return json_encode( $response );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM