One question I do have though, why not just return the query as an object
and use javascript to build <li> list items? This would be less load on the
server, less data to transmit over the interwebs and ultimately faster.



On Thu, Jun 10, 2010 at 9:26 AM, Michael Grant <[email protected]> wrote:

> <!--- get a list of all external customers for AJAX calls --->
> <cffunction name="AJAXgetAllCustomers" access="remote" output="false"
> returnformat="json">
>
> <!--- create a struct to hold our local variables. --->
> <cfset var loc = structNew()>
>
> <!--- returns a query. --->
> <cfset loc.custList = getAllCustomers() />
>  <!--- start string --->
> <cfset cList = "" />
>  <!--- build string from query results. one LI per record --->
> <cfloop query="loc.custList">
>  <cfset cList &= "<li>" />
> <cfset cList &= Trim(loc.custList.full_name) />
>  <cfset cList &= " - " />
> <cfset cList &= Trim(loc.custList.addr_line1) />
>  <cfset cList &= ", " />
> <cfset cList &= Trim(loc.custList.city) />
>  <cfset cList &= ", " />
> <cfset cList &= Trim(loc.custList.state) />
>  <cfset cList &= "</li>" />
> </cfloop>
>
>  <!--- Set struct value to string --->
> <cfset loc.rtnContent = cList />
>
> <!--- return final serialized customer list --->
> <cfreturn SerializeJSON(Trim(loc.rtnContent))>
>    </cffunction>
>
>
> On Wed, Jun 9, 2010 at 5:34 PM, Eric Cobb <[email protected]> wrote:
>
>>
>> I can't figure this thing out for the life of me, and I know it's got to
>> be something simple that I'm missing.
>>
>> Here's what I'm trying to do.  I'm using jQuery to hit a CFC and return
>> data.  The CFC actually queries the database, loops through results and
>> creates list items for each row.  I then return the list items to
>> jQuery, which populates my UL with them.  Here's my problems and what
>> I've done so far:
>>
>>    * returning the data from my CFC as a string causes jQuery to
>>      actually display the html code in the brower, so I literally see
>>      "<li>Eric Cobb</li>".
>>    * returning the data as JSON shows the list items correctly, but my
>>      page is littered with "\\n\\t\\t\\t\\t" at the end of every record.
>>    * I used a regex to clean up all of the line breaks, carriage
>>      returns, and tabs in my CFC before returning to jQuery and that
>> works.
>>    * In every record that contains a slash "/" in the data, the slash
>>      has been replaced with "\\\/".
>>    * Nothing I do can get rid of these slashes.  I've tried RegEx,
>>      SerializeJSON(), and JSStringFormat() in CF, and a whole whoost of
>>      jQuery escape(), html(), text(), and replace() functions, and
>>      nothing works.
>>
>> I think my biggest problem is that I've staring at and fighting with
>> this thing for too long.
>>
>> Here's my method:
>>
>>    <!--- get a list of all external customers for AJAX calls --->
>>    <cffunction name="AJAXgetAllCustomers" access="remote"
>> output="false" returnformat="json">
>>
>>        <!--- create a struct to hold our local variables. --->
>>        <cfset var loc = structNew()>
>>
>>        <!--- returns a query. --->
>>        <cfset loc.custList = getAllCustomers() />
>>
>>        <cfsavecontent variable="loc.rtnContent">
>>            <cfoutput query="loc.custList">
>>                <li>#Trim(loc.custList.full_name)# -
>> #Trim(loc.custList.addr_line1)#, #Trim(loc.custList.city)#,
>> #Trim(loc.custList.state)#
>>            </cfoutput>
>>        </cfsavecontent>
>>
>>        <!--- remove all line breaks, carriage returns, and tabs. --->
>>        <cfset loc.rtnContent =
>> ReReplace(loc.rtnContent,"[#chr(10)#|#chr(13)#|#chr(9)#]","","ALL")>
>>
>>        <cfreturn SerializeJSON(Trim(loc.rtnContent))>
>>    </cffunction>
>>
>>
>> And here is my jQuery call:
>>
>>        <script type="text/javascript">
>>         $(document).ready(function(){
>>
>> $('#contentdiv').load('myCFC.cfc?method=AJAXgetAllCustomers');
>>         });
>>        </script>
>>
>> So, can someone please tell me how to escape the characters so that I
>> can get rid of the "\\\/" in my display output?  And while I'm on the
>> subject, surely there has got to be a better way than having to manually
>> escape every character individually that may cause problems.  I
>> shouldn't have to run regex and whatnot to make everything work.  In my
>> mind, SerializeJSON() (or returnformat="json" for that matter) should
>> have everything escaped and formatted nicely for jQuery, who should then
>> know how to parse everything and display is correctly.  Am I wrong?
>>
>> --
>>
>> Thanks,
>>
>> Eric Cobb
>> ECAR Technologies, LLC
>> http://www.ecartech.com
>> http://www.cfgears.com
>>
>>
>>
>> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology-Michael-Dinowitz/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:334452
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to