Copy link to clipboard
Copied
I have a JQuery Autocomplete working with a *.cfm file but I'm struggling trying to convert that file to a CFC. I'm sure I'm close but could use some pointers so that I know what't going on. Or maybe the cfc is totally wrong and I'm way off base.
Lookup.cfm
<cfset Results = [] />
<cfset LookupInformation = "" />
<cftry>
<cfquery name="Lookup">
select idnumber,
lastname,
firstname,
middlename,
suffix,
tagname,
city,
state
from Registrant
where lower(LastName) like <cfqueryparam value="#LCase(URL.term)#%" cfsqltype="cf_sql_varchar">
order by LastName
</cfquery>
<cfcatch type="any">
</cfcatch>
</cftry>
<cfloop query="Lookup">
<cfset LookupInformation = "(" & idnumber & ")" & " "
& lastname & ", "
& firstname & " "
& middlename & " "
& suffix & " "
& tagname & " "
& city & " "
& state />
<cfset LookupStruct = StructNew() />
<cfset LookupStruct["id"] = idnumber />
<cfset LookupStruct["firstname"] = firstname />
<cfset LookupStruct["middlename"] = middlename />
<cfset LookupStruct["lastname"] = lastname />
<cfset LookupStruct["suffix"] = suffix />
<cfset LookupStruct["tagname"] = tagname />
<cfset LookupStruct["city"] = city />
<cfset LookupStruct["state"] = state />
<cfset LookupStruct["label"] = LookupInformation />
<cfset LookupStruct["value"] = lastname />
<cfset ArrayAppend(Results, LookupStruct) />
</cfloop>
<cfoutput>
#serializeJSON(Results)#
</cfoutput>
Lookup.cfc
<cfcomponent displayname="Lookup" output="no">
<cffunction name="Lookup" access="remote" returntype="array" output="no">
<cfargument name="Term" type="string" required="false">
<cfset var Results = [] />
<cfset var LookupInformation = "" />
<cftry>
<cfquery name="Lookup">
select IDnumber,
LastName,
FirstName,
MiddleName,
Suffix,
Tagname,
City,
State
from Registrant
where lower(LastName) like <cfqueryparam value="#LCase(Term)#%" cfsqltype="cf_sql_varchar">
order by LastName,
FirstName
</cfquery>
<cfcatch type="any">
<cfset arrayAppend(Results, "No results could be found.")>
</cfcatch>
</cftry>
<cfif Lookup.recordcount is not 0>
<cfset LookupInformation = "(" & IDnumber & ")" & " "
& LastName & ", "
& FirstName & " "
& MiddleName & " "
& Suffix & " "
& Tagname & " "
& City & " "
& State />
<cfset LookupStruct = StructNew() />
<cfset LookupStruct["id"] = IDnumber />
<cfset LookupStruct["firstname"] = FirstName />
<cfset LookupStruct["middlename"] = MiddleName />
<cfset LookupStruct["lastname"] = LastName />
<cfset LookupStruct["suffix"] = Suffix />
<cfset LookupStruct["tagname"] = Tagname />
<cfset LookupStruct["city"] = City />
<cfset LookupStruct["state"] = State />
<cfset LookupStruct["label"] = LookupInformation />
<cfset LookupStruct["value"] = LastName />
<cfset ArrayAppend(Results, LookupStruct) />
</cfif>
<cfreturn Results>
</cffunction>
</cfcomponent>
Could someone point out what I'm doing wrong? Wrong returnType? I need to add a serializeJSON to the output?
With Lookup.cfm
$("#RegistrationLastName").autocomplete({
source: "lookup.cfm",
With Lookup.cfc
$("#RegistrationLastName").autocomplete({
source: /CFC/Lookup.cfc?method=Lookup&returnFormat=JSON",
Copy link to clipboard
Copied
What do you see when you use Firebug and compare the two results?
Copy link to clipboard
Copied
Haven't used Firebug. The Lookup.cfm file returns results on the page. The Lookup.cfc file doesn't return anything and reports the IDNumber is an error. Not sure what that is about.
Copy link to clipboard
Copied
Check this video:
http://insideria.com/2009/06/quick-video-example-of-firebug.html
It tells you how to use Firebug to debug the responses sent back from
the server.
Copy link to clipboard
Copied
Just went through that.
I get a 500 Internal Server Error when running the Lookup.cfc from the URL. Throwing the query into SQL returns results so the query is good. I get
Not sure what to think.
Copy link to clipboard
Copied
Comment out all of the code in the Lookup.cfc and put in a simple value and it worked. What is that telling me? My query is not in the correct format to be returned?
<cfset var Results = [] />
<cfset ArrayAppend(Results, "ColdFusion") />
<cfreturn Results>
<wddxPacket version='1.0'><header/><data><array length='1'><string>ColdFusion</string></array></data
></wddxPacket>
Copy link to clipboard
Copied
In your CFM, you were inside a cfloop query=. Therefore you could
reference the db column. You forgot that in your CFC.
Copy link to clipboard
Copied
Doh! How silly of me. Thta's what I get for cutting and pasting. Totally overlooked it. All this time I was thinking it was the returnFormat or I had to do something special with the output.
Thanks.