首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据对象(jquery插件)没有用AJAX填充表,返回有效的JSON

数据对象(jquery插件)没有用AJAX填充表,返回有效的JSON
EN

Stack Overflow用户
提问于 2016-05-02 19:07:11
回答 1查看 82关注 0票数 2

我试图用DataTable jquery填充这个DataTable,使用从php脚本接收这个JSON的AJAX调用,

来自服务器的响应:

代码语言:javascript
复制
{
    "newsletters": [{
        "anio": "2016",
        "mes": "1",
        "quincena": "1"
    }, {
        "anio": "2016",
        "mes": "1",
        "quincena": "2"
    }]
}

HTML文件:

代码语言:javascript
复制
<div id="tabla_newsletters" >
     <table id="newsletter_datatable">
         <thead>
            <tr>
                <th>anio</th>
                <th>mes</th>
                <th>quincena</th>
            </tr>
        </thead>
        <tbody>

        </tbody>

     </table>
</div>


<script>
$(document).ready(function(){   


            var table = $('#newsletter_datatable').DataTable( {
                ajax: {
                    url: '/newsletter/getNewsletters',
                    dataSrc: 'newsletters'
                },
                columns:[
                          { 'newsletters': 'anio' },
                          { 'newsletters': 'mes' },
                          { 'newsletters': 'quincena' }
                    ],    
            } );

    });
</script>

然后,我的php服务(在symfony 1.4中创建),正如我前面所述,是根据JSON在线验证器返回正确的JSON:

代码语言:javascript
复制
 public function executeGetNewsletters(sfWebRequest $request){

    $conn = Doctrine_Manager::getInstance()->getCurrentConnection();

    $qry=$conn->execute("Select anio,mes,quincena from newsletters");

            $newsletters = $qry;

            $dataNews = array();
            $i=0;
                foreach ($newsletters as $news)
                    {

                           $dataNews[$i] = array(
                                "anio" => $news['anio'],
                                "mes" => $news['mes'],
                                "quincena" => $news['quincena'],
                            );
                      ++$i;
                    }

            $output = array(
                "newsletters" => $dataNews,
            );

            $json=$this->renderText(json_encode($output));
            return $json;




  }

Datatable引发以下错误:

"DataTables警告:表id=newsletter_datatable -请求行0的未知参数'0‘。有关此错误的更多信息,请参见http://datatables.net/tn/4

我看过其他案件,但乍一看,它们是不同的.

编辑:我已经解决了it....the问题是在服务器上的脚本上,索引需要是numbers....now --它正在正确地填充表

最终代码:

服务器上的-Response现在是(显然插件只接受我在其他人的bug中看到的这种格式):

代码语言:javascript
复制
{"newsletters":[["2016","1","1"],["2016","1","2"]]}

-HTML文件代码是:

代码语言:javascript
复制
<div id="tabla_newsletters" >
     <table id="newsletter_datatable">
         <thead>
            <tr>
                <th>anio</th>
                <th>mes</th>
                <th>quincena</th>
            </tr>
        </thead>
        <tbody>

        </tbody>

     </table>
</div>


<script>
$(document).ready(function(){   


            var table = $('#newsletter_datatable').DataTable( {
                ajax: {
                    url: '/newsletter/getNewsletters',
                    dataSrc: 'newsletters'
                }
            } );

    });
</script>

而php服务代码是(我只将关联数组的索引从字段名更改为数字):

代码语言:javascript
复制
public function executeGetNewsletters(sfWebRequest $request){

    $conn = Doctrine_Manager::getInstance()->getCurrentConnection();

    $qry=$conn->execute("Select anio,mes,quincena from newsletters");

            $newsletters = $qry;

            $dataNews = array();
            $i=0;
                foreach ($newsletters as $news)
                    {

                           $dataNews[$i] = array(
                                "0" => $news['anio'],
                                "1" => $news['mes'],
                                "2" => $news['quincena'],
                            );
                      ++$i;
                    }

            $output = array(
                "newsletters" => $dataNews,
            );

            $json=$this->renderText(json_encode($output));
            return $json;




  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-03 06:06:41

FYI :你本可以通过正确的方法解决原来的问题:

代码语言:javascript
复制
columns:[
  { data: 'anio' }, //NOT  { 'newsletters': 'anio' }
  { data: 'mes' },
  { data: 'quincena' }
],  

使用data定义应该进入列中的每个新闻稿项中的哪个属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36989812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档