Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help building JSON data using ColdFusion 2

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
Hi,
I'm trying to follow faq1600-6409 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
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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
 
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
 
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
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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
 
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
 
There must be a difference between the output above and the output you're getting, then... Can you post the exact responseText?



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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
 
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
 
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
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi

Lyndon said:
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top