$.ajax ColdFusion cfc JSON Hello World
- by cf_PhillipSenn
I've simplified this example as much as I can. I have a remote function:
<cfcomponent output="false">
<cffunction name="Read" access="remote" output="false">
<cfset var local = {}>
<cfquery name="local.qry" datasource="myDatasource">
SELECT PersonID,FirstName,LastName FROM Person
</cfquery>
<cfreturn local.qry>
</cffunction>
</cfcomponent>
And using the jQuery $.ajax method, I would like to make an unordered list of everyone.
<!DOCTYPE HTML>
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript">
jQuery(function($){
$.ajax({
url: "Remote/Person.cfc?method=Read&ReturnFormat=json",
success: function(data){
var str = '<ul>';
// This is where I need help:
for (var I=0; I<data.length; I++) {
str += '<li>' + I + data[I][1]+ '</li>'
}
str += '</ul>';
$('body').html(str);
},
error: function(ErrorMsg){
console.log("Error");
}
});
});
</script>
</head>
<body>
</body>
</html>
The part where I'm lost is where I'm looping over the data.
I prefer to the use jQuery $.ajax method because I understand that $.get and $.post don't have error trapping.
I don't know how to handle JSON returned from the cfc.