Coldfusion CFC Return JSON display in Jquery, how do I handle multiple records from CFC and display in Jquery?
I need a way to grab the json return from coldfusion and display it in jquery
I can display the first result fine, but if more that one record comes back I am stuck
Here is my cfc
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
Here is my out put to jquery, not sure if this is a good way to do this, but.... this is how I handle mutliple records right now.
<cfset var user = structNew()/>
<cfset thenumber = 1>
<cfloop query="users">
<cfset user["newrequestor#theNumber#"] = users.requestor/>
<cfset user["newrequestorusername#theNumber#"] = users.requestor_username/>
<cfset user["newrequestorphone#theNumber#"] = users.requestorphone/>
<cfset user["newrequestoremail#theNumber#"] = users.requestoremail/>
<cfset user["newthedate#theNumber#"] = users.thedate/>
<cfset user["newapproved#theNumber#"] = users.approved/>
<cfset user["newcomments#theNumber#"] = users.comments/>
<cfset user["newviewed#theNumber#"] = users.viewed/>
<cfset thenumber = thenumber + 1>
</cfloop>
<cfreturn user>
End of CFC
Here is my jquery, I have it manually set to grab first record... not sure how to loop over to get all records returned.
Here I pass my arguments to the cfc to get my result. works great
thedata = instance.getSearch($("#therequestor").val(), $("#fromDate").val(), $("#toDate").val(), $("#theapproved").val(), $("#theroom").val());
Next I build a row in jquery to handle my first record, manual setting not dynamic.
var new_Return = '<tr id="newReturn"><th style="text-align:left;" id="first">Request Date:</th><td>'+thedata.newthedate1+'</td><td>    </td><th style="text-align:left;" id="first">Requestor:</th><td>'+thedata.newrequestor1+'</td><td>    </td><th style="text-align:left;" id="first">Approved:</th><td>'+thedata.newapproved1+'</td><td>    </td><th style="text-align:left;" id="first">Viewed:</th><td>'+thedata.newviewed1+'</td></tr>';
$("#theReturnFormTable").append(new_Return)
Displays first result in my div tag great, how can I loop over if I have multiple records
Example
thedata.newrequestor1 is my first record
then I could have more many more
thedata.newrequestor2 is my 2nd
thedata.newrequestor3 is my 3rd etc. on and on
How can I handle this in jquery, or do I have to start different in coldfusion cfc??
jquery coldfusion cfc
add a comment |
I need a way to grab the json return from coldfusion and display it in jquery
I can display the first result fine, but if more that one record comes back I am stuck
Here is my cfc
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
Here is my out put to jquery, not sure if this is a good way to do this, but.... this is how I handle mutliple records right now.
<cfset var user = structNew()/>
<cfset thenumber = 1>
<cfloop query="users">
<cfset user["newrequestor#theNumber#"] = users.requestor/>
<cfset user["newrequestorusername#theNumber#"] = users.requestor_username/>
<cfset user["newrequestorphone#theNumber#"] = users.requestorphone/>
<cfset user["newrequestoremail#theNumber#"] = users.requestoremail/>
<cfset user["newthedate#theNumber#"] = users.thedate/>
<cfset user["newapproved#theNumber#"] = users.approved/>
<cfset user["newcomments#theNumber#"] = users.comments/>
<cfset user["newviewed#theNumber#"] = users.viewed/>
<cfset thenumber = thenumber + 1>
</cfloop>
<cfreturn user>
End of CFC
Here is my jquery, I have it manually set to grab first record... not sure how to loop over to get all records returned.
Here I pass my arguments to the cfc to get my result. works great
thedata = instance.getSearch($("#therequestor").val(), $("#fromDate").val(), $("#toDate").val(), $("#theapproved").val(), $("#theroom").val());
Next I build a row in jquery to handle my first record, manual setting not dynamic.
var new_Return = '<tr id="newReturn"><th style="text-align:left;" id="first">Request Date:</th><td>'+thedata.newthedate1+'</td><td>    </td><th style="text-align:left;" id="first">Requestor:</th><td>'+thedata.newrequestor1+'</td><td>    </td><th style="text-align:left;" id="first">Approved:</th><td>'+thedata.newapproved1+'</td><td>    </td><th style="text-align:left;" id="first">Viewed:</th><td>'+thedata.newviewed1+'</td></tr>';
$("#theReturnFormTable").append(new_Return)
Displays first result in my div tag great, how can I loop over if I have multiple records
Example
thedata.newrequestor1 is my first record
then I could have more many more
thedata.newrequestor2 is my 2nd
thedata.newrequestor3 is my 3rd etc. on and on
How can I handle this in jquery, or do I have to start different in coldfusion cfc??
jquery coldfusion cfc
Confused.. "user" above is a struct not a query... so you will have a flat struct with "n" number of keys and each key name will contain a digit. This seems kind of kludgy to me (no offense). After all a query IS an array of structs - so it would have user[2]['viewed'] already in it... why would you need to reengineer it?
– Mark A Kruger
Apr 10 '12 at 20:15
Similar to what Mark says I think you will find this easier if you use serializeJSON() on the query and then loop through that.
– Sam Farmer
Apr 10 '12 at 20:17
Not sure why I did, so I should skip the struct all together just return the query
– user1253239
Apr 10 '12 at 20:18
You can simply return the query, justconsole.log
it so that you can inspect it's structure and reference it properly.
– Kevin B
Apr 10 '12 at 20:19
add a comment |
I need a way to grab the json return from coldfusion and display it in jquery
I can display the first result fine, but if more that one record comes back I am stuck
Here is my cfc
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
Here is my out put to jquery, not sure if this is a good way to do this, but.... this is how I handle mutliple records right now.
<cfset var user = structNew()/>
<cfset thenumber = 1>
<cfloop query="users">
<cfset user["newrequestor#theNumber#"] = users.requestor/>
<cfset user["newrequestorusername#theNumber#"] = users.requestor_username/>
<cfset user["newrequestorphone#theNumber#"] = users.requestorphone/>
<cfset user["newrequestoremail#theNumber#"] = users.requestoremail/>
<cfset user["newthedate#theNumber#"] = users.thedate/>
<cfset user["newapproved#theNumber#"] = users.approved/>
<cfset user["newcomments#theNumber#"] = users.comments/>
<cfset user["newviewed#theNumber#"] = users.viewed/>
<cfset thenumber = thenumber + 1>
</cfloop>
<cfreturn user>
End of CFC
Here is my jquery, I have it manually set to grab first record... not sure how to loop over to get all records returned.
Here I pass my arguments to the cfc to get my result. works great
thedata = instance.getSearch($("#therequestor").val(), $("#fromDate").val(), $("#toDate").val(), $("#theapproved").val(), $("#theroom").val());
Next I build a row in jquery to handle my first record, manual setting not dynamic.
var new_Return = '<tr id="newReturn"><th style="text-align:left;" id="first">Request Date:</th><td>'+thedata.newthedate1+'</td><td>    </td><th style="text-align:left;" id="first">Requestor:</th><td>'+thedata.newrequestor1+'</td><td>    </td><th style="text-align:left;" id="first">Approved:</th><td>'+thedata.newapproved1+'</td><td>    </td><th style="text-align:left;" id="first">Viewed:</th><td>'+thedata.newviewed1+'</td></tr>';
$("#theReturnFormTable").append(new_Return)
Displays first result in my div tag great, how can I loop over if I have multiple records
Example
thedata.newrequestor1 is my first record
then I could have more many more
thedata.newrequestor2 is my 2nd
thedata.newrequestor3 is my 3rd etc. on and on
How can I handle this in jquery, or do I have to start different in coldfusion cfc??
jquery coldfusion cfc
I need a way to grab the json return from coldfusion and display it in jquery
I can display the first result fine, but if more that one record comes back I am stuck
Here is my cfc
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
Here is my out put to jquery, not sure if this is a good way to do this, but.... this is how I handle mutliple records right now.
<cfset var user = structNew()/>
<cfset thenumber = 1>
<cfloop query="users">
<cfset user["newrequestor#theNumber#"] = users.requestor/>
<cfset user["newrequestorusername#theNumber#"] = users.requestor_username/>
<cfset user["newrequestorphone#theNumber#"] = users.requestorphone/>
<cfset user["newrequestoremail#theNumber#"] = users.requestoremail/>
<cfset user["newthedate#theNumber#"] = users.thedate/>
<cfset user["newapproved#theNumber#"] = users.approved/>
<cfset user["newcomments#theNumber#"] = users.comments/>
<cfset user["newviewed#theNumber#"] = users.viewed/>
<cfset thenumber = thenumber + 1>
</cfloop>
<cfreturn user>
End of CFC
Here is my jquery, I have it manually set to grab first record... not sure how to loop over to get all records returned.
Here I pass my arguments to the cfc to get my result. works great
thedata = instance.getSearch($("#therequestor").val(), $("#fromDate").val(), $("#toDate").val(), $("#theapproved").val(), $("#theroom").val());
Next I build a row in jquery to handle my first record, manual setting not dynamic.
var new_Return = '<tr id="newReturn"><th style="text-align:left;" id="first">Request Date:</th><td>'+thedata.newthedate1+'</td><td>    </td><th style="text-align:left;" id="first">Requestor:</th><td>'+thedata.newrequestor1+'</td><td>    </td><th style="text-align:left;" id="first">Approved:</th><td>'+thedata.newapproved1+'</td><td>    </td><th style="text-align:left;" id="first">Viewed:</th><td>'+thedata.newviewed1+'</td></tr>';
$("#theReturnFormTable").append(new_Return)
Displays first result in my div tag great, how can I loop over if I have multiple records
Example
thedata.newrequestor1 is my first record
then I could have more many more
thedata.newrequestor2 is my 2nd
thedata.newrequestor3 is my 3rd etc. on and on
How can I handle this in jquery, or do I have to start different in coldfusion cfc??
jquery coldfusion cfc
jquery coldfusion cfc
edited Nov 29 '12 at 22:24
James A Mohler
7,145123354
7,145123354
asked Apr 10 '12 at 19:55
user1253239user1253239
991213
991213
Confused.. "user" above is a struct not a query... so you will have a flat struct with "n" number of keys and each key name will contain a digit. This seems kind of kludgy to me (no offense). After all a query IS an array of structs - so it would have user[2]['viewed'] already in it... why would you need to reengineer it?
– Mark A Kruger
Apr 10 '12 at 20:15
Similar to what Mark says I think you will find this easier if you use serializeJSON() on the query and then loop through that.
– Sam Farmer
Apr 10 '12 at 20:17
Not sure why I did, so I should skip the struct all together just return the query
– user1253239
Apr 10 '12 at 20:18
You can simply return the query, justconsole.log
it so that you can inspect it's structure and reference it properly.
– Kevin B
Apr 10 '12 at 20:19
add a comment |
Confused.. "user" above is a struct not a query... so you will have a flat struct with "n" number of keys and each key name will contain a digit. This seems kind of kludgy to me (no offense). After all a query IS an array of structs - so it would have user[2]['viewed'] already in it... why would you need to reengineer it?
– Mark A Kruger
Apr 10 '12 at 20:15
Similar to what Mark says I think you will find this easier if you use serializeJSON() on the query and then loop through that.
– Sam Farmer
Apr 10 '12 at 20:17
Not sure why I did, so I should skip the struct all together just return the query
– user1253239
Apr 10 '12 at 20:18
You can simply return the query, justconsole.log
it so that you can inspect it's structure and reference it properly.
– Kevin B
Apr 10 '12 at 20:19
Confused.. "user" above is a struct not a query... so you will have a flat struct with "n" number of keys and each key name will contain a digit. This seems kind of kludgy to me (no offense). After all a query IS an array of structs - so it would have user[2]['viewed'] already in it... why would you need to reengineer it?
– Mark A Kruger
Apr 10 '12 at 20:15
Confused.. "user" above is a struct not a query... so you will have a flat struct with "n" number of keys and each key name will contain a digit. This seems kind of kludgy to me (no offense). After all a query IS an array of structs - so it would have user[2]['viewed'] already in it... why would you need to reengineer it?
– Mark A Kruger
Apr 10 '12 at 20:15
Similar to what Mark says I think you will find this easier if you use serializeJSON() on the query and then loop through that.
– Sam Farmer
Apr 10 '12 at 20:17
Similar to what Mark says I think you will find this easier if you use serializeJSON() on the query and then loop through that.
– Sam Farmer
Apr 10 '12 at 20:17
Not sure why I did, so I should skip the struct all together just return the query
– user1253239
Apr 10 '12 at 20:18
Not sure why I did, so I should skip the struct all together just return the query
– user1253239
Apr 10 '12 at 20:18
You can simply return the query, just
console.log
it so that you can inspect it's structure and reference it properly.– Kevin B
Apr 10 '12 at 20:19
You can simply return the query, just
console.log
it so that you can inspect it's structure and reference it properly.– Kevin B
Apr 10 '12 at 20:19
add a comment |
3 Answers
3
active
oldest
votes
You could just call the remote function and let ColdFusion serialize the query object to JSON? (notice returnFormat="JSON")
<cffunction name="getUsers" access="remote" returnType="query" returnFormat="JSON">
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
<cfreturn users>
</cffunction>
JSON formatting will look like this:
"COLUMNS":["NEWREQUESTER","NEWREQUESTERUSERNAME"],"DATA":[["1","JOHN DOE"],["2","JIM DOE"]]
add a comment |
Store your users in an array and return that array.
<cfset var userArr = arrayNew(1)/>
<cfloop query="users">
<cfset var user = structNew()/>
<cfloop list="#users.columnlist#" index="column">
<cfset user[column] = users[column]>
</cfloop>
<cfset ArrayAppend(userArr, user)/>
</cfloop>
<cfreturn userArr />
That creates an array of objects in javascript like this:
[
newrequestrr:"foobar",
newrequestorusername:"foobar",
...
,
...
]
Update: I made code a little more dynamic. I use a custom function in my udf library to make this conversion from query structure to array of rowstructs, I modified it to use your variables.
2
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
1
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
add a comment |
If you want something with the normal json structure, you are going to have to create it. If you're battling with CORS, PM me. I am using this on an Angular 7 front end. Remove the returnType="query"
from your cffunction tag; it just returns the half baked quasi json format. Hope this helps someone.
<cffunction name="getRecipies" access="remote" returnFormat="JSON"
<cfquery name="qryRecipies" datasource="#VARIABLES._dsnName#" username="#VARIABLES._dsnUsername#" password="#VARIABLES._dsnPassword#">
SELECT *
FROM recipes
</cfquery>
<cfset prepArr = arrayNew(1)/>
<cfloop query="qryRecipies">
<cfset prep = structNew()>
<cfset prep["id"] = qryRecipies.id>
<cfset prep["name"] = qryRecipies.name>
<cfset prep["description"] = qryRecipies.description>
<cfset prep["imagePath"] = qryRecipies.imagePath>
<cfset ArrayAppend(prepArr, prep)/>
</cfloop>
<cfreturn serializeJSON(prepArr) />
</cffunction>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10095520%2fcoldfusion-cfc-return-json-display-in-jquery-how-do-i-handle-multiple-records-f%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could just call the remote function and let ColdFusion serialize the query object to JSON? (notice returnFormat="JSON")
<cffunction name="getUsers" access="remote" returnType="query" returnFormat="JSON">
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
<cfreturn users>
</cffunction>
JSON formatting will look like this:
"COLUMNS":["NEWREQUESTER","NEWREQUESTERUSERNAME"],"DATA":[["1","JOHN DOE"],["2","JIM DOE"]]
add a comment |
You could just call the remote function and let ColdFusion serialize the query object to JSON? (notice returnFormat="JSON")
<cffunction name="getUsers" access="remote" returnType="query" returnFormat="JSON">
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
<cfreturn users>
</cffunction>
JSON formatting will look like this:
"COLUMNS":["NEWREQUESTER","NEWREQUESTERUSERNAME"],"DATA":[["1","JOHN DOE"],["2","JIM DOE"]]
add a comment |
You could just call the remote function and let ColdFusion serialize the query object to JSON? (notice returnFormat="JSON")
<cffunction name="getUsers" access="remote" returnType="query" returnFormat="JSON">
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
<cfreturn users>
</cffunction>
JSON formatting will look like this:
"COLUMNS":["NEWREQUESTER","NEWREQUESTERUSERNAME"],"DATA":[["1","JOHN DOE"],["2","JIM DOE"]]
You could just call the remote function and let ColdFusion serialize the query object to JSON? (notice returnFormat="JSON")
<cffunction name="getUsers" access="remote" returnType="query" returnFormat="JSON">
<cfquery name="users" datasource="#thedb#">
In this query I can get 1 record to 25 or even more results
</cfquery>
<cfreturn users>
</cffunction>
JSON formatting will look like this:
"COLUMNS":["NEWREQUESTER","NEWREQUESTERUSERNAME"],"DATA":[["1","JOHN DOE"],["2","JIM DOE"]]
answered Apr 10 '12 at 20:34
Josh SiokJosh Siok
88648
88648
add a comment |
add a comment |
Store your users in an array and return that array.
<cfset var userArr = arrayNew(1)/>
<cfloop query="users">
<cfset var user = structNew()/>
<cfloop list="#users.columnlist#" index="column">
<cfset user[column] = users[column]>
</cfloop>
<cfset ArrayAppend(userArr, user)/>
</cfloop>
<cfreturn userArr />
That creates an array of objects in javascript like this:
[
newrequestrr:"foobar",
newrequestorusername:"foobar",
...
,
...
]
Update: I made code a little more dynamic. I use a custom function in my udf library to make this conversion from query structure to array of rowstructs, I modified it to use your variables.
2
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
1
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
add a comment |
Store your users in an array and return that array.
<cfset var userArr = arrayNew(1)/>
<cfloop query="users">
<cfset var user = structNew()/>
<cfloop list="#users.columnlist#" index="column">
<cfset user[column] = users[column]>
</cfloop>
<cfset ArrayAppend(userArr, user)/>
</cfloop>
<cfreturn userArr />
That creates an array of objects in javascript like this:
[
newrequestrr:"foobar",
newrequestorusername:"foobar",
...
,
...
]
Update: I made code a little more dynamic. I use a custom function in my udf library to make this conversion from query structure to array of rowstructs, I modified it to use your variables.
2
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
1
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
add a comment |
Store your users in an array and return that array.
<cfset var userArr = arrayNew(1)/>
<cfloop query="users">
<cfset var user = structNew()/>
<cfloop list="#users.columnlist#" index="column">
<cfset user[column] = users[column]>
</cfloop>
<cfset ArrayAppend(userArr, user)/>
</cfloop>
<cfreturn userArr />
That creates an array of objects in javascript like this:
[
newrequestrr:"foobar",
newrequestorusername:"foobar",
...
,
...
]
Update: I made code a little more dynamic. I use a custom function in my udf library to make this conversion from query structure to array of rowstructs, I modified it to use your variables.
Store your users in an array and return that array.
<cfset var userArr = arrayNew(1)/>
<cfloop query="users">
<cfset var user = structNew()/>
<cfloop list="#users.columnlist#" index="column">
<cfset user[column] = users[column]>
</cfloop>
<cfset ArrayAppend(userArr, user)/>
</cfloop>
<cfreturn userArr />
That creates an array of objects in javascript like this:
[
newrequestrr:"foobar",
newrequestorusername:"foobar",
...
,
...
]
Update: I made code a little more dynamic. I use a custom function in my udf library to make this conversion from query structure to array of rowstructs, I modified it to use your variables.
edited Apr 10 '12 at 21:36
answered Apr 10 '12 at 20:18
Kevin BKevin B
85.8k11138161
85.8k11138161
2
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
1
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
add a comment |
2
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
1
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
2
2
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
Good answer but overkill. A query already is an array. Just serialize that bad boy :)
– Mark A Kruger
Apr 10 '12 at 20:35
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
@MarkAKruger Agreed.
– Kevin B
Apr 10 '12 at 20:36
1
1
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Isn't it in a weird format though? A struct of arrays rather than an array of structs?
– Aidan Kane
Apr 10 '12 at 21:12
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.
– Kevin B
Apr 10 '12 at 21:30
add a comment |
If you want something with the normal json structure, you are going to have to create it. If you're battling with CORS, PM me. I am using this on an Angular 7 front end. Remove the returnType="query"
from your cffunction tag; it just returns the half baked quasi json format. Hope this helps someone.
<cffunction name="getRecipies" access="remote" returnFormat="JSON"
<cfquery name="qryRecipies" datasource="#VARIABLES._dsnName#" username="#VARIABLES._dsnUsername#" password="#VARIABLES._dsnPassword#">
SELECT *
FROM recipes
</cfquery>
<cfset prepArr = arrayNew(1)/>
<cfloop query="qryRecipies">
<cfset prep = structNew()>
<cfset prep["id"] = qryRecipies.id>
<cfset prep["name"] = qryRecipies.name>
<cfset prep["description"] = qryRecipies.description>
<cfset prep["imagePath"] = qryRecipies.imagePath>
<cfset ArrayAppend(prepArr, prep)/>
</cfloop>
<cfreturn serializeJSON(prepArr) />
</cffunction>
add a comment |
If you want something with the normal json structure, you are going to have to create it. If you're battling with CORS, PM me. I am using this on an Angular 7 front end. Remove the returnType="query"
from your cffunction tag; it just returns the half baked quasi json format. Hope this helps someone.
<cffunction name="getRecipies" access="remote" returnFormat="JSON"
<cfquery name="qryRecipies" datasource="#VARIABLES._dsnName#" username="#VARIABLES._dsnUsername#" password="#VARIABLES._dsnPassword#">
SELECT *
FROM recipes
</cfquery>
<cfset prepArr = arrayNew(1)/>
<cfloop query="qryRecipies">
<cfset prep = structNew()>
<cfset prep["id"] = qryRecipies.id>
<cfset prep["name"] = qryRecipies.name>
<cfset prep["description"] = qryRecipies.description>
<cfset prep["imagePath"] = qryRecipies.imagePath>
<cfset ArrayAppend(prepArr, prep)/>
</cfloop>
<cfreturn serializeJSON(prepArr) />
</cffunction>
add a comment |
If you want something with the normal json structure, you are going to have to create it. If you're battling with CORS, PM me. I am using this on an Angular 7 front end. Remove the returnType="query"
from your cffunction tag; it just returns the half baked quasi json format. Hope this helps someone.
<cffunction name="getRecipies" access="remote" returnFormat="JSON"
<cfquery name="qryRecipies" datasource="#VARIABLES._dsnName#" username="#VARIABLES._dsnUsername#" password="#VARIABLES._dsnPassword#">
SELECT *
FROM recipes
</cfquery>
<cfset prepArr = arrayNew(1)/>
<cfloop query="qryRecipies">
<cfset prep = structNew()>
<cfset prep["id"] = qryRecipies.id>
<cfset prep["name"] = qryRecipies.name>
<cfset prep["description"] = qryRecipies.description>
<cfset prep["imagePath"] = qryRecipies.imagePath>
<cfset ArrayAppend(prepArr, prep)/>
</cfloop>
<cfreturn serializeJSON(prepArr) />
</cffunction>
If you want something with the normal json structure, you are going to have to create it. If you're battling with CORS, PM me. I am using this on an Angular 7 front end. Remove the returnType="query"
from your cffunction tag; it just returns the half baked quasi json format. Hope this helps someone.
<cffunction name="getRecipies" access="remote" returnFormat="JSON"
<cfquery name="qryRecipies" datasource="#VARIABLES._dsnName#" username="#VARIABLES._dsnUsername#" password="#VARIABLES._dsnPassword#">
SELECT *
FROM recipes
</cfquery>
<cfset prepArr = arrayNew(1)/>
<cfloop query="qryRecipies">
<cfset prep = structNew()>
<cfset prep["id"] = qryRecipies.id>
<cfset prep["name"] = qryRecipies.name>
<cfset prep["description"] = qryRecipies.description>
<cfset prep["imagePath"] = qryRecipies.imagePath>
<cfset ArrayAppend(prepArr, prep)/>
</cfloop>
<cfreturn serializeJSON(prepArr) />
</cffunction>
edited Feb 13 at 4:02
Nathan Strutz
7,37613148
7,37613148
answered Nov 15 '18 at 5:27
Ticka EllemTicka Ellem
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10095520%2fcoldfusion-cfc-return-json-display-in-jquery-how-do-i-handle-multiple-records-f%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Confused.. "user" above is a struct not a query... so you will have a flat struct with "n" number of keys and each key name will contain a digit. This seems kind of kludgy to me (no offense). After all a query IS an array of structs - so it would have user[2]['viewed'] already in it... why would you need to reengineer it?
– Mark A Kruger
Apr 10 '12 at 20:15
Similar to what Mark says I think you will find this easier if you use serializeJSON() on the query and then loop through that.
– Sam Farmer
Apr 10 '12 at 20:17
Not sure why I did, so I should skip the struct all together just return the query
– user1253239
Apr 10 '12 at 20:18
You can simply return the query, just
console.log
it so that you can inspect it's structure and reference it properly.– Kevin B
Apr 10 '12 at 20:19