Coldfusion jQuery getJSON:获取WDDX而不是JSON

我使用Brian Rinaldi的coldfusion函数将dotnet webservice数据集转换为查询结构.然后将每个查询作为 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-ified结果.这有点好用,因为返回了有效的JSON,但它仍然包含在< wddxPacket>中.标签,如下:

<wddxPacket version='1.0'><header/><data><string>
{
    "recordcount": 31,"columnlist": "callcharge,callreference,connduration,conntype,settingcount,startdate,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",jsonEncode:true // the cfc converts query to JSON
         },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>

解决方法

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

快速浏览一下jQuery源代码,可以看出getJSON()只是get(),其中JSON属性已经硬编码:

getJSON: function( url,data,callback ) {
  return jQuery.get(url,callback,"json");
}

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...