在ColdFusion中将JSON数据填充到Jquery数据表时出现问题

时间:2017-10-20 14:21:36

标签: coldfusion

我正在使用coldFusion 2016,当我将json数据放入我的Jquery数据表时,我遇到了一个问题。数据表仅显示处理消息。 JSON结果似乎没有错误,但我不知道是什么问题。

这是我的数据表实现

<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet">

<script type="text/javascript">
var table = '';
$(document).ready(function() {
   table = $('#example').DataTable( {
	    "bProcessing": true,
       "bServerSide": true,
		   "ajax": "uploadProcess.cfc?method=getDetails&partyId=100004",
		"sPaginationType": "full_numbers",
		"oLanguage": {
				 "sProcessing":   "Wait please...",
				 "sZeroRecords":  "No records found.",
				 "sInfo":         "Users from _START_ to _END_ of _TOTAL_ total",
			     "sInfoEmpty":    "Users from 0 to 0 of 0 total"
				},
		"aoColumns": [
                        { "data": "ID" },
                        { "data": "ORG_NAME" },
                        { "data": "TYPE" },
                        { "data": "PATH" },
                        { "data": "URL" },
                        { "data": "DELETE" }
                     ]
    });
 });
</script>
</head>
<body>
<div id="dataDiv">
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Id</th>
                <th>File Name</th>
                <th>Type</th>
                <th>Path</th>
				        <th>Preview</th>
				        <th>Delete</th>
            </tr>
        </thead>
    </table>
 </div>
</body>

这是我的uploadProcess.cfc

<cfcomponent>
<cffunction name="getDetails" access="remote" returnFormat="json">
	<cfargument name="partyId" type="string" required="yes">
	<cfparam name="arguments.iDisplayStart" default="0">
	<cfparam name="arguments.iDisplayLength" default="10">
	<cfparam name="arguments.iSortCol_0" default="UploadFileID">
	<cfparam name="arguments.sSortDir_0" default="ASC">
	<cfparam name="arguments.sEcho" default="1">
	
	<cfstoredproc procedure="get_upload_file_details" datasource="standout">
	<cfprocparam value="#partyId#" cfsqltype="CF_SQL_INT">
	<cfprocparam value="#arguments.iDisplayStart#" cfsqltype="CF_SQL_INT">
	<cfprocparam value="#arguments.iDisplayLength#" cfsqltype="CF_SQL_INT">
	<cfprocparam value="#arguments.iSortCol_0#" cfsqltype="CF_SQL_VARCHAR">
	<cfprocparam value="#arguments.sSortDir_0#" cfsqltype="CF_SQL_VARCHAR">
	<cfprocresult name="getUploadDtls">
	</cfstoredproc>

	<cfset userArray = arrayNew(1)>	
	
	<cfloop query="getUploadDtls">
	<cfif UserSessionID eq "">
    <cfset deleteLink = "<span class='delete-link link'>Delete</span>" />
    <cfelse>
    <cfset deleteLink = "">
    </cfif>
		<cfset userStruct = {}>
		<cfset userStruct.ID = UploadFileID>
		<cfset userStruct.ORG_NAME = OriginalFileName>
		<cfset userStruct.GEN_NAME = SystemFileName>
		<cfset userStruct.TYPE = DocumentName>
		<cfset userStruct.PATH = FilePath>
		<cfset userStruct.URL = "<a href='renderpdf.cfm?path=#FilePath#&name=#OriginalFileName#' target='_blank'>Preview</a>">
		<cfset userStruct.DELETE = deleteLink>		
		<cfset arrayAppend(userArray, userStruct) >
	</cfloop>
	 <cfif getUploadDtls.RecordCount GT 0>
     <cfset firstRow = queryGetRow(getUploadDtls,1)>
	 <cfset record_count = firstRow.record_count>
	 <cfelse>
	 <cfset record_count = 0>
     </cfif>
	<cfset returnStruct = {}>
	<cfset returnStruct['iTotalRecords'] = record_count>
	<cfset returnStruct['iTotalDisplayRecords'] = record_count>
	<cfset returnStruct['sEcho'] = arguments.sEcho>
	<cfset returnStruct['aaData'] = userArray>
	<cfset resultsJSON = SerializeJSON(returnStruct)>
	<cfreturn resultsJSON>
</cffunction>
</cfcomponent>

从我的cffunction返回的Json结果如下所示。

{""aaData"":[{""GEN_NAME"":""sample_489.pdf"",""PATH"":""C://Standout/web_uploads/100004/Medical Reports/sample_489.pdf"",""DELETE"":"""",""ORG_NAME"":""sample.pdf"",""ID"":77,""TYPE"":""Medical Report"",""URL"":""<a href='renderpdf.cfm?path=C://Standout/web_uploads/100004/Medical Reports/sample_489.pdf&name=sample.pdf' target='_blank'>Preview</a>""}],""iTotalDisplayRecords"":1,""iTotalRecords"":1,""sEcho"":1}"

我无法弄清楚有人可以提供什么问题?

1 个答案:

答案 0 :(得分:0)

首先,我会删除那些用于包裹结构的花括号 如果你传入你的结构,SerializeJson()已经很好用了。

您的JSON似乎无效,可能导致错误,或者您的数据表未正确配置。

老实说,我建议您只使用cfloop填充表格。

<cfloop query="myQuery">
   <tr>
       <td>
          #myQuery.someData#
       </td>
       ...
   </tr>
</cfloop>

如果您在页面加载时加载数据,此解决方案也可以正常工作 数据表API有时会被命中或错过。