.cfc page not supported in ColdFusion 5, make it work using .cfm page

Posted by Mahavir on December 23, 2011
  0 comments

Recently i use ColdFusion 9 for development, one of the project in that i create findcity.cfc page that contains function 'findCity()' to get list of cities(with zipcode) of specified state.

Code of findcity.cfc is look like:




<cfcomponent displayname="findcity" output="false">
    <cffunction name="findCity" access="remote" returntype="query">
        <cfargument name="state" type="any" required="true">
        <cfquery name="cityResult" datasource="#Application.ds#" username="#Application.UserName#" password="#Application.Password#">
            select cityname, zipcode from cityZip
            where statecode = <cfqueryparam value="#arguments.state#" cfsqltype="cf_sql_varchar"> 
            group by cityname
            order by cityname
        </cfquery>
        <cfreturn cityResult />        
    </cffunction>
</cfcomponent>

I use one javascript function that send Ajax request that call 'findCity()' funciton.

Code look like:




function search_cities(id,state)
    {
        $.ajax({      
                type: "GET",
                url: "/cfc/findcity.cfc?method=findCity&state="+state,
                dataType: "json",
                success: function(data){     // callback function that fill 'City' comboBox
                    var i=0;
                    objCity = $("#"+id)[0];
                    objCity.options.length = data.recordcount+1;
                    objCity.options[0].text =" -Select City-";
                    objCity.options[0].value = "";
                    objCity.options[0].selected = true;
                    for(i=0 ; i < data.recordcount ; ++i)
                    {
                        objCity.options[i+1].text =data.data.cityname[i];
                        objCity.options[i+1].value = data.data.cityname[i];
                    }
                }
            });
    }



Issue is that project build on ColdFusion 5, so that not support .cfc pages.

For that i create 'searchCity.cfm' page that provide the same fuctionality same as 'findCity()' function of findcity.cfc page

Code of searchCity.cfm is look like:




<cfquery name="cityResult" datasource="#Application.ds#" username="#Application.UserName#" password="#Application.Password#">
    select cityname, zipcode from cityZip
    where statecode = <cfqueryparam value="#url.state#" cfsqltype="cf_sql_varchar"> 
    group by cityname
    order by cityname
</cfquery>

<!--- converting Query resultSet in to json format --->
<cfset dData = cityResult>
<cfset dJSONString = "" />
<cfset recordcountKey = "recordcount" />
<cfset columnlistKey = "columnlist" />
<cfset columnlist = LCase(dData.columnlist) />
<cfset dataKey = "data" />
    
<cfset dJSONString = dJSONString & '"#recordcountKey#":' & dData.recordcount />
<cfset dJSONString = dJSONString & ',"#columnlistKey#":"' & columnlist & '"' />
<cfset dJSONString = dJSONString & ',"#dataKey#":' />

<cfset dJSONString = dJSONString & "{" />
<cfset colPos = 1 />
<cfloop list="#cityResult.columnlist#" delimiters="," index="column">
    <cfif colPos GT 1>
        <cfset dJSONString = dJSONString & "," />
    </cfif>
    <cfset column = LCase(column) />
    <cfset dJSONString = dJSONString & '"' & column & '":[' />
    <cfloop from="1" to="#cityResult.recordcount#" index="i">
        <cfif i GT 1>
            <cfset dJSONString = dJSONString & "," />
        </cfif>
        <cfset dJSONString = dJSONString & '"' & cityResult[column][i] & '"'/>
    </cfloop>
    
    <cfset dJSONString = dJSONString &  "]" />
    <cfset colPos = colPos + 1 />
</cfloop>
<cfset dJSONString = dJSONString & "}" />
<cfoutput>{#dJSONString#}</cfoutput>



Now, call 'searchCity.cfm' page to fill 'City' comboBox using Ajax by setting 'url' attribute same as below,




url:"/cfc/searchCity.cfm?state="+state;



Hope this is helpful.

Comments


Leave a comment

Tell us about yourself
Your Name     (required field)
Your Email Address     (required field)
Website URL      
Comment and preferences
Your Comment  
   
  Subscribe to this comment thread