CFC handling jQuery.serializeArray() data via AJaX post
Copy link to clipboard
Copied
Hello, all,
I've got a form that I'm using AJaX to submit after using jQuery.serializeArray() and JSON.stringify(). I've set the contentType to "application/json". What ColdFusion gets in the CFC is an array of structs. Screencap included.
<cfset requestBody = toString(getHttpRequestData().content) />
What I would _like_ to do is convert this to the form scope. But converting to a query is just as good. Question is: What is the most efficient way to do this?
V/r,
^ _ ^
Copy link to clipboard
Copied
Well, I don't know if this is the most efficient way, but I did fashion a method to do it. Feels kind of hackish. If anyone else knows a better way, PLEASE, I am all about learning something new / fresh.
In the CFC, at the top of the function that performs form validation/sanitization, I added the following (before a cftry tag):
<cfset form = {} />
<cfset requestBody = DeserializeJSON(toString(getHttpRequestData().content)) />
<cfset aryLen = ArrayLen(requestBody) />
<cfloop index="idx" from="1" to="#val(aryLen)#" step="1">
<cfif isStruct(requestBody[idx]) AND Structcount(requestBody[idx])>
<cfset form[requestBody[idx].name] = requestBody[idx].value />
</cfif>
</cfloop>
<cfdump var="#form#" />
.. and it works.
V/r and HTH,
^ _ ^
Copy link to clipboard
Copied
Fine. I would call it requestData instead of form, to avoid any possible confusion down the line. For example, the existence of the form scope usually implies the existence of a referrer.

