JS中的Uncaught SyntaxError:Unexpected token}

时间:2017-08-15 20:20:58

标签: javascript jquery datatables

这个错误是什么意思,代码有什么问题?我尝试执行,我不知道它的含义是什么?我错过了什么?是否有逗号丢失,因为我看起来并重新查看并且没有找到错误,无论它说它有什么。

无论如何,我尝试在每个地方都添加逗号,无论如何都是如此。

<script type="text/javascript" language="javascript" class="init">
    var editor; // use a global for the submit and return data rendering in the examples

    $(document).ready(function() {
        editor = new $.fn.dataTable.Editor( {
            ajax: "../php/staff.php",
            table: "#publicationTable",
            fields: [ {
                    label: "Visualização:",
                    name: "p.status"
                }, {
                    label: "Título da Publicação:",
                    name: "ti.PublicationTitle"
                }, {
                    label: "Tipo de Publicação:",
                    name: "ty.PublicationType"
                }, {
                    label: "Ano:",
                    name: "p.ano"
                }, {
                    label: "Mês:",
                    name: "p.competencia"
                }, {
                    label: "Empresa:",
                    name: "c.razaoSocial"
                }, {
                    label: "Favorecido:",
                    name: "e.nome"
                }
            ]
        } );

        var table = $('#publicationTable').DataTable( {
        lengthChange: true,
        ajax: "../php/staff.php",
        columns: [
            {   data: "p.status",
                render: function ( data, type, row ) {
                    var text = "";
                    if (type == "display") {
                        if (data == "1") {
                            text = "<i class='ace-icon fa fa-rocket'></i>";
                        } else {
                            text = "<i class='ace-icon fa fa-circle'></i>";
                        }
                        data = text
                    }
                    return data;
                },
            },
                { data: "ti.PublicationTitle" },
                { data: "ty.PublicationType" },
                { data: "p.ano" },
                { data: "p.competencia" },
                { data: "c.razaoSocial" },
                { data: "e.nome" },
                {
                            data: "p.id_Publication",
                            render: function(data, type, full){
                                return '<a data-toggle="modal" data-target="#infoModal" data-id="' + full[7] + '" id="getPublication" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company=' + full[7] + '"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="' + full[7] + '" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';
                            }}
                }
                }
            ],
        } );
    } );

    </script>

4 个答案:

答案 0 :(得分:0)

你的问题在于:

{
    data: 'p.id_Publication',
    render: function(data, type, full) {
        return '<a data-toggle="modal" ...></i></a>';
    }} // remove these
} //this now closes the render function
}

进行此更改后:

{
    data: 'p.id_Publication',
    render: function(data, type, full) {
        return '<a data-toggle="modal" ...></i></a>';
    }
}

答案 1 :(得分:0)

DataTables中的columns属性还有一个

  

}

使用此代码块

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
  editor = new $.fn.dataTable.Editor({
    ajax: "../php/staff.php",
    table: "#publicationTable",
    fields: [{
      label: "Visualização:",
      name: "p.status"
    }, {
      label: "Título da Publicação:",
      name: "ti.PublicationTitle"
    }, {
      label: "Tipo de Publicação:",
      name: "ty.PublicationType"
    }, {
      label: "Ano:",
      name: "p.ano"
    }, {
      label: "Mês:",
      name: "p.competencia"
    }, {
      label: "Empresa:",
      name: "c.razaoSocial"
    }, {
      label: "Favorecido:",
      name: "e.nome"
    }]
  });

  var table = $('#publicationTable').DataTable({
    lengthChange: true,
    ajax: "../php/staff.php",
    columns: [{
      data: "p.status",
      render: function(data, type, row) {
        var text = "";
        if (type == "display") {
          if (data == "1") {
            text = "<i class='ace-icon fa fa-rocket'></i>";
          } else {
            text = "<i class='ace-icon fa fa-circle'></i>";
          }
          data = text
        }
        return data;
      },
    }, {
      data: "ti.PublicationTitle"
    }, {
      data: "ty.PublicationType"
    }, {
      data: "p.ano"
    }, {
      data: "p.competencia"
    }, {
      data: "c.razaoSocial"
    }, {
      data: "e.nome"
    }, {
      data: "p.id_Publication",
      render: function(data, type, full) {
        return '<a data-toggle="modal" data-target="#infoModal" data-id="' + full[7] + '" id="getPublication" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company=' + full[7] + '"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="' + full[7] + '" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';
      }
    }],
  });
});

你可以去提琴手粘贴你的代码并点击Tidy(顶部栏)来对齐它,这样你就可以轻松找到问题。

答案 2 :(得分:0)

你在结尾附近有一些自由浮动的右括号。

另外,我在第4行

上取出了new
 var editor; // use a global for the submit and return data rendering in the examples

    $(document).ready(function() {
        editor = $.fn.dataTable.Editor( {
            ajax: "../php/staff.php",
            table: "#publicationTable",
            fields: [ {
                    label: "Visualização:",
                    name: "p.status"
                }, {
                    label: "Título da Publicação:",
                    name: "ti.PublicationTitle"
                }, {
                    label: "Tipo de Publicação:",
                    name: "ty.PublicationType"
                }, {
                    label: "Ano:",
                    name: "p.ano"
                }, {
                    label: "Mês:",
                    name: "p.competencia"
                }, {
                    label: "Empresa:",
                    name: "c.razaoSocial"
                }, {
                    label: "Favorecido:",
                    name: "e.nome"
                }
            ]
        } );

        var table = $('#publicationTable').DataTable( {
        lengthChange: true,
        ajax: "../php/staff.php",
        columns: [
            {   data: "p.status",
                render: function ( data, type, row ) {
                    var text = "";
                    if (type == "display") {
                        if (data == "1") {
                            text = "<i class='ace-icon fa fa-rocket'></i>";
                        } else {
                            text = "<i class='ace-icon fa fa-circle'></i>";
                        }
                        data = text
                    }
                    return data;
                },
            },
                { data: "ti.PublicationTitle" },
                { data: "ty.PublicationType" },
                { data: "p.ano" },
                { data: "p.competencia" },
                { data: "c.razaoSocial" },
                { data: "e.nome" },
                {
                            data: "p.id_Publication",
                            render: function(data, type, full){
                                return '<a data-toggle="modal" data-target="#infoModal" data-id="' + full[7] + '" id="getPublication" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company=' + full[7] + '"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="' + full[7] + '" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';

                }
                }
            ],
        } );
    } );

答案 3 :(得分:0)

这可以修复由不正确的括号引起的意外令牌错误。意外的令牌错误是语法错误。 期望一种特定的语言结构,但提供了其他东西。这可能是一个简单的拼写错误 - Mozilla Developer

&#13;
&#13;
 <script type="text/javascript" language="javascript" class="init">
    var editor; // use a global for the submit and return data rendering in the examples

    $(document).ready(function() {
    editor = new $.fn.dataTable.Editor({
      ajax: "../php/staff.php",
      table: "#publicationTable",
      fields: [{
        label: "Visualização:",
        name: "p.status"
      }, {
        label: "Título da Publicação:",
        name: "ti.PublicationTitle"
      }, {
        label: "Tipo de Publicação:",
        name: "ty.PublicationType"
      }, {
        label: "Ano:",
        name: "p.ano"
      }, {
        label: "Mês:",
        name: "p.competencia"
      }, {
        label: "Empresa:",
        name: "c.razaoSocial"
      }, {
        label: "Favorecido:",
        name: "e.nome"
      }]
    });

    var table = $('#publicationTable').DataTable({
        lengthChange: true,
        ajax: "../php/staff.php",
        columns: [
          {
            data: "p.status",
            render: function(data, type, row) {
              var text = "";
              if (type == "display") {
                if (data == "1") {
                  text = "<i class='ace-icon fa fa-rocket'></i>";
                } else {
                  text = "<i class='ace-icon fa fa-circle'></i>";
                }
                data = text
              }
              return data;
            },
          }, 
          {
            data: "ti.PublicationTitle"
          }, 
          {
            data: "ty.PublicationType"
          }, 
          {
            data: "p.ano"
          }, 
          {
            data: "p.competencia"
          }, 
          {
            data: "c.razaoSocial"
          }, 
          {
            data: "e.nome"
          }, 
          {
            data: "p.id_Publication",
            render: function(data, type, full) {
              return '<a data-toggle="modal" data-target="#infoModal" data-id="' + full[7] + '" id="getPublication" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company=' + full[7] + '"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="' + full[7] + '" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';
            }
          }
      ],
    });
    });
    
  </script>
&#13;
&#13;
&#13;