“无法读取未定义的属性'长度'。”使用ColdFusion和jQuery DataTable

时间:2017-09-04 12:32:46

标签: jquery json ajax coldfusion

我正在开发一个ColdFusion页面,它创建了一个应该获取JSON数据的jQuery DataTable。

为此我已经在jQuery中初始化了DataTable,并给出了获取数据的正确URL,但尽管如此我得到了错误

  

未捕获的TypeError:无法读取未定义的属性“length”。

1。 allCLient.cfm(返回JSON数据)

{
    "COLUMNS": ["CID", "NAME", "EMAIL", "CONTACT", "DOB", "GENDER", "QUALIFICATION", "DESIGNATION", "CITY", "DISTRICT", "MSTATUS", "PHOTO"],
    "DATA": [
        [39, "ramesh", "ramesh@gmail.com", 9494949491, "October, 07 2017 00:00:00", "Male", "MBA", "Tester", "delhi", "mumbai", "unmarried", null],
        [47, "neeraj", "neeraj@gmail.com", 9369369369, "June, 22 2017 00:00:00", "Male", "BE", "Tester", "UP", "UP", "married", null],
        [51, "deepak", "deepak@gmail.com", 8877556699, "August, 19 2017 00:00:00", "Male", "MCA", "Developer", "bhilai", "bhilai", "unmarried", null],
        [59, "vishwa", "vishwa@gmail.com", 8787878787, "October, 08 2017 00:00:00", "Male", "BCA", "Engineer", "durg", "mumbai", "unmarried", null],
        [60, "vivek", "vivek@gmail.com", 8965896589, "August, 17 2017 00:00:00", "Male", "BE", "Engineer", "jagdalpur", "jagdalpur", "married", null],
        [61, "tapas", "tapas@gmail.com", 8082858881, "November, 04 2017 00:00:00", "Male", "MCA", "Tester", "bilaspur", "bilaspur", "unmarried", null],
        [78, "vasu", "vas54@gmail.com", 8877887788, "June, 09 2017 00:00:00", "Male", "MCA", "Engineer", "bhilai", "durg", "unmarried", "ColdFusionTutorial.jpg"],
        [84, "devesh", "devesh@gmail.com", 9494949494, "February, 09 2017 00:00:00", "Male", "MCA", "Developer", "raipur", "raipur", "unmarried", "ColdFusionTutorial.jpg"],
        [88, "deepesh", "deepesh@gmail.com", 8978897889, "September, 14 2017 00:00:00", "Male", "BE", "Developer", "bhilai", "durg", "unmarried", "ColdFusionTutorial.jpg"]
    ]
}

2。 Javascript(jQuery)

$(document).ready(function() {
     $('#example').DataTable({        
         "ajax": "allClient.cfm",
         "columns": [
            { "data": "name" },
            { "data": "email" },
            { "data": "contact" },
            { "data": "designation" },
            { "data": "city" }            
        ]
    });
});

3。 HTML

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Contact</th>
        <th>Designation</th>
        <th>City</th>
    </tr>
    </thead>
</table>

4。 CFML代码

  <cfquery datasource="cfprogrammer" name="getClient" result="cust">
        select * from client </cfquery>

 <cfif cust.recordcount gt 0>   


 <cfset myArr = ArrayNew(1)>

 <cfloop query = "getClient">
    <cfset mystrct = StructNew()>
    <cfset StructInsert(mystrct,'name',getClient.name)>
    <cfset StructInsert(mystrct,'email',getClient.email)>
    <cfset StructInsert(mystrct,'contact',getClient.contact)>
    <cfset StructInsert(mystrct,'designation',getClient.designation)>
    <cfset StructInsert(mystrct,'city',getClient.city)>

     <cfset ArrayAppend(myArr, mystrct)>
 </cfloop>

   <!--- <cfdump var="#myArr#">--->
   <cfset mystrct1 = StructNew()>
    <cfset StructInsert(mystrct1,'data',myArr)>
   <cfset myJSONvar = serializeJSON(mystrct1)>
   <cfoutput>#myJSONvar#</cfoutput>

<cfelse>
      Failed            
</cfif>

1 个答案:

答案 0 :(得分:3)

当ColdFusion将查询对象转换为JSON时,默认情况下结果的结构如下:

{
    "COLUMNS": ["COLNAME-1", "COLNAME-2", "COLNAME-N"],
    "DATA": [
        ["row-1-data-1", "row-1-data-2", "row-1-data-N"],
        ["row-2-data-1", "row-2-data-2", "row-2-data-N"],
        ["row-3-data-1", "row-3-data-2", "row-3-data-N"]
    ]
}

但jQuery DataTable需要这种格式,一个简单的对象数组:

[
    {
        "COLNAME-1": "row-1-data-1", 
        "COLNAME-2": "row-1-data-2", 
        "COLNAME-N": "row-1-data-N"
    },
    {
        "COLNAME-1": "row-2-data-1", 
        "COLNAME-2": "row-2-data-2", 
        "COLNAME-N": "row-2-data-N"
    },
    {
        "COLNAME-1": "row-3-data-1", 
        "COLNAME-2": "row-3-data-2", 
        "COLNAME-N": "row-3-data-N"
    }
]

ColdFusion可以使用SerializeJSON() function的特殊参数创建第二种格式(此参数已在CF 2016中添加,更新2):

<cfset jsonData = SerializeJSON(yourQuery, "struct")>

现在取决于allCLient.cfm的样子。我怀疑你只需拨打SerializeJSON()并输出结果。在这种情况下,上述就足够了。

不要忘记设置<cfcontent type="application/json">

另外,正如评论中所指出的,您需要注意ColdFusion不区分大小写,并且默认为大写列名。因此,您的JSON也将具有大写键。 Javascript区分大小写,这意味着必须在那里使用大写形式。