ColdFusion转换ASP.Net Webservice:想要JSON,但得到WDDX

时间:2010-12-23 21:29:47

标签: jquery json coldfusion wddx

我正在使用cfc将ASP.NET Web服务数据转换为coldfusion查询对象。我在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
    });
}

我可以确认它是 IS 从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>

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

这适用于CF8和CF9:在CFFUNCTION中你可以指定:returnFormat =“json”

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

并取回JSON

答案 2 :(得分:0)

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

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

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