0

我正在使用 cfc 将 ASP.NET Web 服务数据转换为冷融合查询对象。我在我的 jquery 代码中使用 getJSON 调用返回该查询对象。但是,返回的数据被格式化为 wddx 数据包而不是 JSON 数据集,并且代码似乎没有完成。不知道我做错了什么。

$(document).ready(function(){
    $("#submitForm").click(function(e){ 
        e.preventDefault();
        e.stopPropagation();
        internetUsage();
    });     
});

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 ALERT NEVER FIRES
    });
}

我可以确认它是从getInternetUsage () 函数(如下)返回的真正格式良好的查询对象。我不知道为什么它作为 WDDX 数据包通过。

<cffunction name="getInternetUsage" access="remote" returnType="any" returnformat="JSON" 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">
        <cfargument name="entity" required="false" default="Table">
        <cfset var qResult = 0>
        <cfset var strWS = invokeInternetUsage(
                        SessionID=arguments.SessionID,
                        CustomerCode=arguments.CustomerCode,
                        FullUserName=arguments.FullUserName,
                        StartDate=arguments.StartDate,
                        EndDate=arguments.EndDate)>
        <cfset var aResult = convertDotNetDataset(strWS)>

        <--- extract the queries from the returned struct ---> 
        <cfif arguments.entity is 'Table'>
            <cfset qResult = aResult.Table>
        <cfelseif arguments.entity is 'Table1'>
            <cfset qResult = aResult.Table1>
        </cfif>
        <cfreturn qResult>
    </cffunction>

    <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 = "">
        <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>
        <cfreturn aTemp>
    </cffunction>


    <!--- convertDotNetDataset --->
    <!--- Converts a dotnet dataset into a structure of queries --->
    <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">
        <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>
    </cfloop>

    <cfreturn Local.result>
</cffunction>

编辑-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

3

您的服务器运行的是什么 Coldfusion 版本?我知道 CFMX7 和更早版本不支持从 cfc 返回 json。在我的经验中,当 CF 遇到未知的返回格式时,它使用默认值 (wddx)。

于 2010-12-23T21:43:14.237 回答
0

您需要告诉我们您使用的 ACF 或 Railo 版本。ACF8有一个bug,调用远程函数时需要删除application.cfc中的onrequest()方法:

http://www.raymondcamden.com/index.cfm/2009/7/13/ColdFusion-9-fixes-onRequest-adds-onCFCRequest

此外,您应该启动 chrome 中的开发人员工具并检查您从 ajax 请求中获得的响应。

于 2012-11-30T01:13:26.350 回答
0

这适用于 CF8 和 CF9:在 CFFUNCTION 中,您可以指定:returnFormat="json"

此外,在任何 CFC 调用中,您应该能够附加 URL 变量:&return_format=json

并取回 JSON

于 2010-12-27T18:37:33.993 回答