9

我正在使用 Brian Rinaldi 的冷融合功能将 dotnet Web 服务数据集转换为查询结构。然后将每个查询作为 JSON 返回到客户端页面,以便在 jQuery 函数中使用。

查询是有效的查询对象。但是,没有返回 JSON。相反,我得到 WDDX 如下:

<wddxPacket version='1.0'>
  <header />
  <data>
    <recordset rowCount='31'
    fieldNames='startdate,starttime,subscribercode,dest_number,description,ConnDuration,Callcharge,Usage,ConnType,pages,CallReference,SettingCount'
    type='coldfusion.sql.QueryTable'>
      <field name='startdate'>
        <string>2010-01-30T00:00:00+13:00</string>
        <string>2010-01-29T00:00:00+13:00</string>
      </field>
    </recordset>
  </data>
</wddxPacket>

使用以下代码:

function internetUsage(){   
    $.getJSON("system.cfc",{
        method:'getInternetUsage',
        SessionID:$("#vSessionID").val(),
        CustomerCode:$("#vCustomerCode").val(),
        FullUserName:$("#selUser").val(),
        StartDate:$("#vStartDate").val(),
        EndDate:$("#vEndDate").val(),
        returnformat:'json',
        queryformat:'column'
             },function(res,code){

         alert('hello');    // THIS NEVER FIRES!    
    });
}

因此,我尝试让 CFC 将查询转换为 JSON 并返回 JSON 化的结果。这工作得更好一点,因为返回了有效的 JSON,但它仍然包含在<wddxPacket>标签中,如下所示:

<wddxPacket version='1.0'><header/><data><string>
{
    "recordcount": 31,
    "columnlist": "callcharge,callreference,connduration,conntype,description,dest_number,pages,settingcount,startdate,starttime,subscribercode,usage",
    "data": [
        {
            "callcharge": "",
            "callreference": "",
            "connduration": 86403,
            "conntype": "UBS",
            "description": "dageorgetti",
            "dest_number": "",
            "pages": "",
            "settingcount": 5,
            "startdate": "2010-01-30T00:00:00+13:00",
            "starttime": "2010-01-30T00:00:00+13:00",
            "subscribercode": "dageorgetti",
            "usage": 33.7300
        }...<snip>...
...<snip>...
</string></data></wddxPacket>

实现上述目标的呼吁如下:

function internetUsage(){   
    $.getJSON("system.cfc",{
        method:'getInternetUsage',
        SessionID:$("#vSessionID").val(),
        CustomerCode:$("#vCustomerCode").val(),
        FullUserName:$("#selUser").val(),
        StartDate:$("#vStartDate").val(),
        EndDate:$("#vEndDate").val(),
        jsonEncode:true // the cfc converts query to JSON
         },function(res,code){

              alert('Hello'); // still not firing       
    });
}

我在 CFC 中使用 returntype="JSON"。cfc 相当复杂,我认为我不需要将其粘贴到此处。我可以确认它肯定会生成有效的查询对象,转换函数似乎成功地将其转换为有效的 JSON。我不知道为什么它会回到包装在 wddxPacket 标签中的客户端。

编辑-CFC

<cffunction name="invokeInternetUsage" access="remote" returnType="any" output="false">
    <cfargument name="SessionID" required="true">
    <cfargument name="CustomerCode" required="true">
    <cfargument name="FullUserName" required="true">
    <cfargument name="StartDate" required="true">
    <cfargument name="EndDate" required="true">
    <cfset var aTemp = "">
    <cftry>
        <cfinvoke 
            webservice="http://Portal/internet.asmx?WSDL"
            method="Usage"
            returnvariable="aTemp">
                <cfinvokeargument name="SessionID" value="#arguments.SessionID#"/>
                <cfinvokeargument name="CustomerCode" value="#arguments.CustomerCode#"/>
                <cfinvokeargument name="FullUserName" value="#arguments.FullUserName#"/>
                <cfinvokeargument name="StartDate" value="#arguments.StartDate#"/>
                <cfinvokeargument name="EndDate" value="#arguments.EndDate#"/>
        </cfinvoke>
        <cfcatch></cfcatch>
    </cftry>
    <cfreturn aTemp>
</cffunction>


<!--- convertDotNetDataset --->
<cffunction name="convertDotNetDataset" access="remote" returnType="any" output="false"
        hint="takes a .Net dataset and converts it to a CF structure of queries">
<cfargument name="dataset" required="true">
<cfset var Local = StructNew()>
<cfset Local.result = structNew() />
<cfset Local.aDataset = arguments.dataset.get_any() />
<cfset Local.xSchema = xmlParse(Local.aDataset[1]) />
<cfset Local.xData = xmlParse(Local.aDataset[2]) />

<!--- Create Queries --->
<cfset Local.xTables = Local.xSchema["xs:schema"]["xs:element"]["xs:complexType"]["xs:choice"] />
<cfloop from="1" to="#arrayLen(Local.xTables.xmlChildren)#" index="Local.i">
    <cfset Local.tableName = Local.xTables.xmlChildren[Local.i].xmlAttributes.name />
    <cfset Local.xColumns = Local.xTables.xmlChildren[Local.i].xmlChildren[1].xmlChildren[1].xmlChildren/>
    <cfset Local.result[Local.tableName] = queryNew("") />
    <cfloop from="1" to="#arrayLen(Local.xColumns)#" index="Local.j">
        <cfif left(Local.xColumns[Local.j].xmlAttributes.name,6) neq 'Column'>
            <cfset queryAddColumn(Local.result[Local.tableName], Local.xColumns[Local.j].xmlAttributes.name, arrayNew(1)) />
        </cfif>
    </cfloop>
</cfloop>

<!--- see if there are any row of data, if not exit --->
<cfif NOT StructKeyExists(Local.xData["diffgr:diffgram"], "NewDataSet")>
    <cfreturn Local.result>
</cfif>

<!--- Populate Queries --->
<cfset Local.xRows = Local.xData["diffgr:diffgram"]["NewDataSet"] />
<cfloop from="1" to="#arrayLen(Local.xRows.xmlChildren)#" index="Local.i">
    <cftry>
<cfset Local.thisRow = Local.xRows.xmlChildren[Local.i] />
        <cfset Local.tableName = Local.thisRow.xmlName />
        <cfset queryAddRow(Local.result[Local.tableName], 1) />
        <cfloop from="1" to="#arrayLen(Local.thisRow.xmlChildren)#" index="Local.j">
            <cfif left(Local.thisRow.xmlChildren[Local.j].xmlName,6) neq 'Column'>
                <cfset querySetCell(Local.result[Local.tableName], Local.thisRow.xmlChildren[Local.j].xmlName, Local.thisRow.xmlChildren[Local.j].xmlText, Local.result[Local.tableName].recordCount) />
            </cfif>
        </cfloop>
        <cfcatch></cfcatch>
    </cftry>
</cfloop>

<cfreturn Local.result>

4

3 回答 3

7

您正在手动构建 JSON,但 cfc 方法将该返回值视为要包装在 WDDX 数据包中的字符串。您应该尝试添加returnformat="plain"到您的 cfc 方法中。另外,您正在使用.getJSON(). 相反,使用.get().

快速浏览一下 jQuery 源代码会发现,getJSON()它只是get()带有已经硬编码的 JSON 属性:

getJSON: function( url, data, callback ) {
  return jQuery.get(url, data, callback, "json");
}
于 2011-01-24T05:44:27.870 回答
2

每当我从 CFC 返回 JSON 数据时,我的函数往往如下所示:

<cffunction name="methodName" access="remote" returnformat="plain" output="false">
<cfset jsonresult = '{
 "somevar1": "val1",
 "somevar2": "val2"}' />

<cfreturn jsonresult/>

于 2011-01-24T00:11:31.790 回答
0

尝试通过浏览器运行 CFC。

我第一次尝试<cffunction name="myFunction" returnFormat="JSON">在 CFC 中也获得了 WDDX。然后我将其更改为纯文本并直接通过浏览器运行它,但它返回了错误。然后再次改成JSON,用浏览器查看。它按预期返回了 JSON。

于 2011-12-03T00:49:14.907 回答