进程json从coldfusion cfc返回

时间:2010-12-04 17:05:30

标签: jquery json coldfusion cfc

您好我有一个表格选择绑定到cfc - 我可以看到json返回但它没有显示在我的表单上 - 我有下面的代码希望有人可以帮助:

FORM

<script type="text/javascript">
// <!--
$(document).ready(function()
{
$("#CountriesList").change(function()
{
if($(this).val() != '')
{
$.ajax({
type: "POST",
url: "mycfc.cfc?method=GetCities&returnformat=json",
data: ({
CountryID: $(this).val()
}),
dataType: "xml",
success: function(xml)
{
$('#Cities option').remove();
$(xml).find('record').each(function()
{
$("#Cities").append('<option value="' + $(this).find('tape_width').text() + '">' + $(this).find('tape_width').text() + '<\/option>');
});
}
});
}
});

})

// -->
</script>

<cfsilent>
<cfinvoke component="mycfc" method="CountriesList" returnvariable="CountriesList"/>
</cfsilent>
 <cfoutput>
 <label for="CountriesList">Select Country:</label>
 <select name="CountriesList" id="CountriesList">
<option value="" selected="selected">...</option>
 <cfloop query="CountriesList"><option value="#BAND#">#CountriesList.BAND#</option></cfloop>
</select>
<label for="Cities">Select City:</label>
<select name="cities" id="Cities"><option value="">...</option></select>
</cfoutput>

实际的CFC如下:

<cfcomponent>
<cffunction access="remote" name="CountriesList" output="false" returntype="query">
<cfquery name="SelectAllCountries" datasource="test">
 SELECT DISTINCT BAND
 FROM FABRICS
 WHERE TYPE='venetian'
 AND isACTIVE='true'
 ORDER BY BAND
</cfquery>
<cfreturn SelectAllCountries>
</cffunction>
<cffunction access="remote" name="GetCities" output="false" returntype="query">
<cfargument name="CountryID" required="yes" type="any" default="" />
<cfquery name="Cities" datasource="test">
 SELECT TAPE_WIDTH
 FROM tapes
 WHERE SLAT_WIDTH='#arguments.CountryID#'
</cfquery>
<cfreturn Cities>
</cffunction>
</cfcomponent>

有人可以指出哪里出错吗?

由于

2 个答案:

答案 0 :(得分:1)

我通过添加queryformat解决了类似的问题,如下所示:

$.getJSON("getContacts.cfc?method=getContacts&returnformat=json&queryFormat=column

在此之前我看到json被退回(通过萤火虫),但它不是戏剧性的......

希望有所帮助

答案 1 :(得分:0)

如果你要返回json,为什么要指定XML数据类型?试试

datatype: 'json'