×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need Help building JSON data using ColdFusion
2

Need Help building JSON data using ColdFusion

Need Help building JSON data using ColdFusion

(OP)
Hi,
I'm trying to follow FAQ1600-6409: What is JSON? and my server side is ColdFusion 5.0.

I'm working on building the responseText string and suspect that is my problem. The error hits when I try to eval() the responseText.

Here is the loop where I try to build the data:

CODE

<cfset dupeCheck='>
        <cfset JSONData='(['>
        <cfoutput query="getLicensee">
            <cfif Trim(getLicensee.NAME) neq dupeCheck>
                <cfset JSONData=JSONData+'{"LicenseeName":"#getLicensee.Name#","SSN":
"#getLicensee.SSN#","LicenseYear":"#getLicensee.Year#"}'>
            </cfif>
            <cfset dupeCheck=Trim(getLicensee.NAME)>
        </cfoutput>
        <cfset JSONData=JSONData+'])'>
The getLicensee.Name field contains a comma, that may be the cause but I'm not sure what to do about that.

I'm very excited about using AJAX/JSON if I can just get over this learning curve...

Thanks very much in advance.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion


The comma shouldn't be a problem because it's inside quotes.

Perhaps showing us the final JSON output instead of server-side code might prove more helpful for debugging, given that that is what the browser is evaluating?

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Need Help building JSON data using ColdFusion


Also, not sure how this CF outputs (see my first post about supplying client-side code), but if this is more than a single-item array, perhaps you need a comma inbetween the array items?

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Need Help building JSON data using ColdFusion

(OP)
The error = "syntax error" on this line:

var Licensee = eval(xmlhttp.responseText);

Server side code:

CODE

function ajaxFunction(urlVar){
var xmlhttp;
if (document.getElementById('lname').value.length < 2){
    document.getElementById('ssn').value='';
    //document.getElementById('LicenseYear').value='';
}
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
 
 var Licensee = eval(xmlhttp.responseText);
 var str='';
 for (var recno in Licensee) {
     str += Licensee[recno].LicenseeName+"<br/>";
 }
  }
}
xmlhttp.open("GET","Nomenu/GetJSON.cfm?searchTerm="+urlVar,true);
xmlhttp.send(null);
}

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion

(OP)
I commented out the eval() line and output the data being returned to responseText.

Response data:

CODE

([{"LicenseeName":"WILBUR, WENDY LEE ","SSN":"111-11-1111","LicenseYear":"2008.0"}{"LicenseeName":"WILCHER, STEPHENIE DIANE","SSN":"222-22-2222","LicenseYear":"2009.0"}{"LicenseeName":"WILCOX, MARVIN LEE ","SSN":"333-33-3333","LicenseYear":"2009.0"}])

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion

(OP)
I corrected the missing comma but still getting the same syntax error on the line:

CODE

var Licensee = eval(xmlhttp.responseText);

Here is the new responseText:

CODE

([{"LicenseeName":"PATE, ARCHIE","SSN":"111-11-1111","LicenseYear":"2007"},{"LicenseeName":"PATE, ERIC MICHAEL","SSN":"111-11-1111","LicenseYear":"2008"},{"LicenseeName":"PATINO, HERCULANO MENDOSA","SSN":"111-11-1111","LicenseYear":"2007"}])

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion


That's odd - if I run this:

CODE

eval(([{"LicenseeName":"PATE, ARCHIE", "SSN":"111-11-1111", "LicenseYear":"2007"}, {"LicenseeName":"PATE, ERIC MICHAEL", "SSN":"111-11-1111", "LicenseYear":"2008"}, {"LicenseeName":"PATINO, HERCULANO MENDOSA", "SSN":"111-11-1111", "LicenseYear":"2007"}]));

I get an array with 3 items, each item being an object with the 3 properties you have.

Even asking for the length property gives me 3... if I enter this in the URL bar of Firefox, I see an alert with "3". What do you see?

CODE

javascript:alert(eval(([{"LicenseeName":"PATE, ARCHIE", "SSN":"111-11-1111", "LicenseYear":"2007"}, {"LicenseeName":"PATE, ERIC MICHAEL", "SSN":"111-11-1111", "LicenseYear":"2008"}, {"LicenseeName":"PATINO, HERCULANO MENDOSA", "SSN":"111-11-1111", "LicenseYear":"2007"}])).length)

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Need Help building JSON data using ColdFusion


Hmm - just a hunch - try clearing your browser cache and running again. My hunch is that you're using IE and you're getting cached results back without the comma.

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Need Help building JSON data using ColdFusion

(OP)
I'm getting 3 elements in IE7 also.  I saw on your code page how to put a random time string in the url to stop the cache problem and I cleared my cache as well.

The syntax error is still there on my eval() function???

I'll run it in foxfire when I get to the office.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion

(OP)
I installed Firefox and Firebug and still get the same syntax error on the eval() function. I'm perplexed.  

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion

(OP)
Here is the exact response text copied as displayed in the browser with this line of code document.getElementById('pickList').innerHTML = xmlhttp.responseText;:

CODE

( [ {"LicenseeName":"WAGGONER, DELBERT","LicenseeSSN":"111-11-1111","LicenseYear":" 2008"},{"LicenseeName":"WAGGONER, DELBERT KENNETH ","LicenseeSSN":"211-11-1111","LicenseYear":" 2011"},{"LicenseeName":"WAGGONER, ELLA JUANITA ","LicenseeSSN":"311-11-1111","LicenseYear":" 2009"} ] )
But when I eval it it errors. If I eval this copied string, see below, then it works fine.

CODE

var test='( [ {"LicenseeName":"ALEXANDER, DAVID NOEL ","LicenseeSSN":"111-11-1111","LicenseYear":" 2009"},{"LicenseeName":"ALEXANDER, FRED A","LicenseeSSN":"211-11-1111","LicenseYear":" 2008"},{"LicenseeName":"ALEXANDER, JEROME","LicenseeSSN":"311-11-1111","LicenseYear":" 2008"} ] ) '
                    var users = eval(test);
                    var str = "";
                    for ( var recno in users ) {
                        str += users[recno].LicenseeSSN + " is ";
                        str += users[recno].LicenseeName + " ";
                        str += users[recno].LicenseYear + "<br/>";
                        }
                    document.getElementById('pickList').innerHTML = str;

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion

(OP)
I wonder if I should be asking this in the coldfusion forum? I hate to double post tho...

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion

(OP)
Here is something intersting..

If I put the data into a textarea element when the server script is running, then eval the value of that element it works great. I've been trying anything I can think of to see why I can't evulate the responseText.  

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

RE: Need Help building JSON data using ColdFusion


Can you post a URL to a test page that exhibits the problem? Without being able to reproduce it, it's really hard to diagnose.

Incidentally, does Firebug not point out where on the line the syntax error occurs?

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Need Help building JSON data using ColdFusion


Sorry - I meant the Fx error console, not Firebug... Click 'Tools -> Error Console' to find out (sometimes it highlights the offending piece of code).

Firebug might also tell you what the syntax error is, e.g. 'Syntax Error: unterminated string literal'
 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Need Help building JSON data using ColdFusion

Hi

Quote (Lyndon):

Here is the exact response text copied as displayed in the browser with this line of code document.getElementById('pickList').innerHTML = xmlhttp.responseText;
While you have FireBug, I would suggest to use it : open the Net panel, expand the line of the AJAX request, open the Response tab. There you can see the raw response data as arrived, with untouched line breaks.

Feherke.
http://rootshell.be/~feherke/

RE: Need Help building JSON data using ColdFusion

(OP)
That did it Feherke! I just installed Firefox and had no idea how to use it. I learned that all of the server side documents are returned as data. It made sense once I saw that. I removed all of the HTML elements and it works great.

Dan, thanks for helping me solve the cache problem in IE.

Thanks to you both.  

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close