Re: Problems with slashes in JSON

2010-06-10 Thread Michael Grant

 !--- 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 cft...@ecartech.com 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
  liEric 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:334450
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


Re: Problems with slashes in JSON

2010-06-10 Thread Eric Cobb

Thanks Brian.  That eliminated all of the RegEx I was having to do, but 
my closing list tag is now being displayed as \\\/li at the end of 
every list item, and all slashes in my text are showing up as \\\/.  
Any idea on how to fix that?


Thanks,

Eric Cobb
ECAR Technologies, LLC
http://www.ecartech.com
http://www.cfgears.com



Brian Kotek wrote:
 Just a note that your list item has no closing tag.

 You can try building up the string with assignments rather than using
 cfsavecontent. i.e.:

 cfoutput query=loc.custList
  cfset local.result = local.result  li#Trim(loc.custList.full_name)#
 - #Trim(loc.custList.addr_line1)#,
 #Trim(loc.custList.city)# #Trim(loc.custList.state)#/li /
 /cfoutput

 On Wed, Jun 9, 2010 at 6:54 PM, Josh Nathanson 
 p...@oakcitygraphics.comwrote:

   
 In your cfsavecontent, get rid of all the tabs and carriage returns.  This
 will make your code less readable, but it should take care of the problem
 you're having.

 So it might end up looking like this:

 cfsavecontent variable=loc.rtnContentcfoutput
 query=loc.custListli#Trim(loc.custList.full_name)# -
 #Trim(loc.custList.addr_line1)#, #Trim(loc.custList.city)#,
 #Trim(loc.custList.state)#/cfoutput/cfsavecontent

 -- Josh


 -Original Message-
 From: Eric Cobb [mailto:cft...@ecartech.com]
 Sent: Wednesday, June 09, 2010 2:35 PM
 To: cf-talk
 Subject: Problems with slashes in JSON


 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
  liEric 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:334451
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


Re: Problems with slashes in JSON

2010-06-10 Thread Michael Grant

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 mgr...@modus.bz 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 cft...@ecartech.com 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
  liEric 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


Re: Problems with slashes in JSON

2010-06-10 Thread Cutter (ColdFusion)

If Eric is returning a plain string, then there is no need to 
SerializeJson() the result, as all he needs is the string. That is why 
Eric is getting the additional slashes in his closing tag, because the 
SerializeJson() method is automatically escaping the slash characters.

I'll agree with Michael though. If you did get a Json return, and just 
parsed it to add the li tags in your doc you'll reduce your 
(unnecessary) server-side overhead. Returning the string is easier, but 
not necessarily best for you application or your code reusability.

Steve Cutter Blades
Adobe Community Professional - ColdFusion
Adobe Certified Professional
Advanced Macromedia ColdFusion MX 7 Developer

Co-Author of Learning Ext JS
http://www.packtpub.com/learning-ext-js/book
_
http://blog.cutterscrossing.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:334453
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


Re: Problems with slashes in JSON

2010-06-10 Thread Michael Grant


 If Eric is returning a plain string, then there is no need
 to SerializeJson() the result


Too right. That was an artifact from using Eric's example.


~|
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:334454
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


Re: Problems with slashes in JSON

2010-06-10 Thread Eric Cobb

I originally thought about just parsing the return in jQuery, until I 
realized what this code was ultimately going to have to do.  The items 
in this list are actually going to wind up having text hyperlinks, form 
buttons, and maybe even images added to them, so for me it's easier to 
build it out in CF and just have jQuery display it.  Too much JavaScript 
hurts my head.  :)

I'm about to the point I'm ready to ditch jQuery and just do what I need 
to do without trying to be all ajaxy with it.

Thanks,

Eric Cobb
ECAR Technologies, LLC
http://www.ecartech.com
http://www.cfgears.com



Cutter (ColdFusion) wrote:
 If Eric is returning a plain string, then there is no need to 
 SerializeJson() the result, as all he needs is the string. That is why 
 Eric is getting the additional slashes in his closing tag, because the 
 SerializeJson() method is automatically escaping the slash characters.

 I'll agree with Michael though. If you did get a Json return, and just 
 parsed it to add the li tags in your doc you'll reduce your 
 (unnecessary) server-side overhead. Returning the string is easier, but 
 not necessarily best for you application or your code reusability.

 Steve Cutter Blades
 Adobe Community Professional - ColdFusion
 Adobe Certified Professional
 Advanced Macromedia ColdFusion MX 7 Developer

 Co-Author of Learning Ext JS
 http://www.packtpub.com/learning-ext-js/book
 _
 http://blog.cutterscrossing.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:334456
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


Re: Problems with slashes in JSON

2010-06-10 Thread Michael Grant

It's about being ajaxy in the right places.

You can be ajaxy without using jQuery as well. If you only want jQuery to do
the ajax calls and all you want to do is dump html (into a div or other dom
object) that's been compiled on the server just write your own to get the
http object?


On Thu, Jun 10, 2010 at 10:50 AM, Eric Cobb cft...@ecartech.com wrote:


 I originally thought about just parsing the return in jQuery, until I
 realized what this code was ultimately going to have to do.  The items
 in this list are actually going to wind up having text hyperlinks, form
 buttons, and maybe even images added to them, so for me it's easier to
 build it out in CF and just have jQuery display it.  Too much JavaScript
 hurts my head.  :)

 I'm about to the point I'm ready to ditch jQuery and just do what I need
 to do without trying to be all ajaxy with it.

 Thanks,

 Eric Cobb
 ECAR Technologies, LLC
 http://www.ecartech.com
 http://www.cfgears.com



 Cutter (ColdFusion) wrote:
  If Eric is returning a plain string, then there is no need to
  SerializeJson() the result, as all he needs is the string. That is why
  Eric is getting the additional slashes in his closing tag, because the
  SerializeJson() method is automatically escaping the slash characters.
 
  I'll agree with Michael though. If you did get a Json return, and just
  parsed it to add the li tags in your doc you'll reduce your
  (unnecessary) server-side overhead. Returning the string is easier, but
  not necessarily best for you application or your code reusability.
 
  Steve Cutter Blades
  Adobe Community Professional - ColdFusion
  Adobe Certified Professional
  Advanced Macromedia ColdFusion MX 7 Developer
 
  Co-Author of Learning Ext JS
  http://www.packtpub.com/learning-ext-js/book
  _
  http://blog.cutterscrossing.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:334457
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


RE: Problems with slashes in JSON

2010-06-09 Thread Josh Nathanson

In your cfsavecontent, get rid of all the tabs and carriage returns.  This
will make your code less readable, but it should take care of the problem
you're having.

So it might end up looking like this:

cfsavecontent variable=loc.rtnContentcfoutput
query=loc.custListli#Trim(loc.custList.full_name)# -
#Trim(loc.custList.addr_line1)#, #Trim(loc.custList.city)#,
#Trim(loc.custList.state)#/cfoutput/cfsavecontent

-- Josh


-Original Message-
From: Eric Cobb [mailto:cft...@ecartech.com] 
Sent: Wednesday, June 09, 2010 2:35 PM
To: cf-talk
Subject: Problems with slashes in JSON


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
  liEric 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:334439
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm


Re: Problems with slashes in JSON

2010-06-09 Thread Brian Kotek

Just a note that your list item has no closing tag.

You can try building up the string with assignments rather than using
cfsavecontent. i.e.:

cfoutput query=loc.custList
 cfset local.result = local.result  li#Trim(loc.custList.full_name)#
- #Trim(loc.custList.addr_line1)#,
#Trim(loc.custList.city)# #Trim(loc.custList.state)#/li /
/cfoutput

On Wed, Jun 9, 2010 at 6:54 PM, Josh Nathanson p...@oakcitygraphics.comwrote:


 In your cfsavecontent, get rid of all the tabs and carriage returns.  This
 will make your code less readable, but it should take care of the problem
 you're having.

 So it might end up looking like this:

 cfsavecontent variable=loc.rtnContentcfoutput
 query=loc.custListli#Trim(loc.custList.full_name)# -
 #Trim(loc.custList.addr_line1)#, #Trim(loc.custList.city)#,
 #Trim(loc.custList.state)#/cfoutput/cfsavecontent

 -- Josh


 -Original Message-
 From: Eric Cobb [mailto:cft...@ecartech.com]
 Sent: Wednesday, June 09, 2010 2:35 PM
 To: cf-talk
 Subject: Problems with slashes in JSON


 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
  liEric 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:334443
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm