Reputation: 101
I am working on a ColdFusion page that creates a jQuery DataTable which should fetch JSON data.
For this I have initialized the DataTable in jQuery and given the proper URL for fetching the data, but in spite of this I get the error
Uncaught TypeError: Cannot read property 'length' of undefined.
{
"COLUMNS": ["CID", "NAME", "EMAIL", "CONTACT", "DOB", "GENDER", "QUALIFICATION", "DESIGNATION", "CITY", "DISTRICT", "MSTATUS", "PHOTO"],
"DATA": [
[39, "ramesh", "[email protected]", 9494949491, "October, 07 2017 00:00:00", "Male", "MBA", "Tester", "delhi", "mumbai", "unmarried", null],
[47, "neeraj", "[email protected]", 9369369369, "June, 22 2017 00:00:00", "Male", "BE", "Tester", "UP", "UP", "married", null],
[51, "deepak", "[email protected]", 8877556699, "August, 19 2017 00:00:00", "Male", "MCA", "Developer", "bhilai", "bhilai", "unmarried", null],
[59, "vishwa", "[email protected]", 8787878787, "October, 08 2017 00:00:00", "Male", "BCA", "Engineer", "durg", "mumbai", "unmarried", null],
[60, "vivek", "[email protected]", 8965896589, "August, 17 2017 00:00:00", "Male", "BE", "Engineer", "jagdalpur", "jagdalpur", "married", null],
[61, "tapas", "[email protected]", 8082858881, "November, 04 2017 00:00:00", "Male", "MCA", "Tester", "bilaspur", "bilaspur", "unmarried", null],
[78, "vasu", "[email protected]", 8877887788, "June, 09 2017 00:00:00", "Male", "MCA", "Engineer", "bhilai", "durg", "unmarried", "ColdFusionTutorial.jpg"],
[84, "devesh", "[email protected]", 9494949494, "February, 09 2017 00:00:00", "Male", "MCA", "Developer", "raipur", "raipur", "unmarried", "ColdFusionTutorial.jpg"],
[88, "deepesh", "[email protected]", 8978897889, "September, 14 2017 00:00:00", "Male", "BE", "Developer", "bhilai", "durg", "unmarried", "ColdFusionTutorial.jpg"]
]
}
$(document).ready(function() {
$('#example').DataTable({
"ajax": "allClient.cfm",
"columns": [
{ "data": "name" },
{ "data": "email" },
{ "data": "contact" },
{ "data": "designation" },
{ "data": "city" }
]
});
});
<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>
<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>
Upvotes: 1
Views: 438
Reputation: 338228
When ColdFusion converts a query object to JSON, the result is structured like this by default:
{
"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"]
]
}
but jQuery DataTable expects this format, a simple array of objects:
[
{
"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 can create the second format with a special parameter to the SerializeJSON()
function (this parameter has been added in CF 2016, update 2):
<cfset jsonData = SerializeJSON(yourQuery, "struct")>
Now it depends on how your allCLient.cfm
looks like. I suspect you simply call SerializeJSON()
and output the result. In that case the above is enough.
Don't forget to set <cfcontent type="application/json">
.
Also, as pointed out in the comments, you need to be aware that ColdFusion is not case-sensitive and defaults to upper-case column names. Therefore your JSON will have upper-case keys as well. Javascript is case-sensitive which means you must use the upper-case form there.
Upvotes: 3