我试图从我的模板表中的响应中检索数据(使用DataTables),但它会抛出以下错误:
DataTables警告:table 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();
}
}
我做错了什么,如何解决?
答案 0 :(得分:1)
您收到的错误表明您返回的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 );